Debugging bad request errors-Windows Live SDK

Share on:

Important note - this code is not to be considered production quality - as always feel free to use it, but please take it with a handful of salt. In my experience working with these sorts of APIs can be a real killer if you’re not extremely careful about how you write your code.

When you’re making calls to the live.com endpoints from .NET code, you’ll probably be doing something like this:

 1
 2var address = "https://consent.live.com/AccessToken.aspx";
 3var postData = String.Format(
 4    "wrap_client_id={0}&wrap_client_secret={1}&wrap_callback={2}&wrap_verification_code={3}&idtype={4}",
 5    myApiKey,
 6    clientSecret,
 7    callBackUrl,
 8    verificationCode,
 9    "CID");
10
11// Create the request
12var request = (HttpWebRequest)WebRequest.Create(address);
13request.Method = "POST";
14request.ContentType = "application/x-www-form-urlencoded";
15
16// Post the data
17var content = Encoding.UTF8.GetBytes(formData);
18request.ContentLength = content.Length;
19using (var requestStream = request.GetRequestStream())
20{
21    requestStream.Write(content, 0, content.Length);
22}
23
24// Read the response
25var response = request.GetResponse();
26if (response != null)
27{
28    using (var responseStream = response.GetResponseStream())
29    {
30        if (responseStream != null)
31        {
32            using (var reader = new StreamReader(responseStream))
33            {
34            // Read the response text and do something with it
35                var responseText = reader.ReadToEnd();
36            }
37        }
38    }
39}

However if you’ve done anything wrong, e.g constructed the request data incorrectly, a 4xx response code will be returned (400 bad request, 401 unauthorized, etc) and when you call request.GetResponse() a WebException will be thrown.

In order to give yourself a fighting chance of debugging this, you’ll need to catch the error and have a look in the response stream - you’ll often find that you’re being given more information than you thought:

 1
 2catch (WebException ex)
 3{
 4    using (var responseStream = ex.Response.GetResponseStream())
 5    {
 6        if (responseStream != null)
 7        {
 8            var responseData = new StreamReader(responseStream).ReadToEnd();
 9            
10            // The response data from the live API services is formatted as
11            // name=value&name2=value2, so we can use the HttpUtility to parse it
12            var responseParameters = HttpUtility.ParseQueryString(responseData);
13            var response = String.Join(
14                "\r\n", 
15                (from k in responseParameters.Keys.OfType<string>() 
16                select k + ":" + responseParameters[k]).ToArray());
17
18            // You'll probably want to create your own exception type!
19            throw new Exception(response, ex);
20        }
21    }
22
23    // Couldn't get the response, so just throw the previous exception
24    throw ex;
25}

Now instead of just getting an exception with an unhelpful message, you will hopefully get something that describes the problem in a bit more detail.

Hope this helps.