Help get this topic noticed by sharing it on Twitter, Facebook, or email.

Getting JSON error code during authorization, using C#

Hi, I wanted to see if anyone had suggestions on how to get the error codes returned during authorization. I am using C#, and trying to have visibility into the codes mentioned here:

Here is the code I'm using

// preparing to get a PIN for example:

const string APP_KEY = "(secret app key)";
string webrequesttext = "" + AuthCode + "&client_id=" + APP_KEY;
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(webrequesttext);
myReq.Method = "POST";
IAsyncResult rs = myReq.BeginGetResponse(new AsyncCallback(GetAccessTokenCallback), myReq);

And this is the GetAccessTokenCallBack, that processes the response

private void GetAccessTokenCallback(IAsyncResult asynchronousResult)
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
string responseString = streamRead.ReadToEnd();
accesstokenobject = JsonConvert.DeserializeObject(responseString);
// accesstokenobject now has all the access info, and this works perfectly when there are no errors. This code generates an exception if the result had an error (for example the PIN has not been authorized)

Where/how can I access the error result that is supposed to be in a JSON payload?

Thank you
1 person has
this question
  • I found the answer, posting it here in case it helps someone else.

    In the return function, after the try/catch, add this to catch WebException errors, and then decode the error.

    catch (WebException ex)
    using (var stream = ex.Response.GetResponseStream())
    using (var reader = new StreamReader(stream))
    string rs = reader.ReadToEnd();
    weberrorcode = JsonConvert.DeserializeObject(rs);
  • (some HTML allowed)
    How does this make you feel?
    Add Image

    e.g. sad, anxious, confused, frustrated kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited