401 Unauthorized when Acquiring an Access Token: Windows Live SDK

Working with other people is hard. Working with new SDK documentation is even harder, especially when it's wrong.

If you're following the Working with OAuth WRAP Windows Live SDK documentation and you get to the Acquiring an Access Token sample you'll probably encounter a 401 unauthorized error when you try and read the response from https://consent.live.com/AccessToken.aspx. This is because the POST data that the sample code gets you to build up is incorrect.

Instead of:

string postData = string.Format(
    "{0}?wrap_client_id={1}&wrap_client_secret={2}&wrap_callback={3}&
     wrap_verification_code={4}&idtype={5}",
    requestUrl,
    appSettings["wll_appid"],
    appSettings["wll_secret"],
    "http://www.fabrikam.com",
    verificationCode,
    "CID");

Use this:

string postData = string.Format(
    "wrap_client_id={0}&wrap_client_secret={1}&wrap_callback={2}&
     wrap_verification_code={3}&idtype={4}",
    appSettings["wll_appid"],
    appSettings["wll_secret"],
    "http://www.fabrikam.com",
    verificationCode,
    "CID");

For some reason the sample code was including the URL the request was going to in the post data, which confuses the server into thinking that no wrap_client_id has been posted.

Hope that helps.

4 Comments

  • Mike said

    Hi Sergio, if you're using the .NET WebRequest class to make the POST request, make sure that: * You've set the Method to "POST" * You've set the ContentType to "application/x-www-form-urlencoded" * You have actually written the post data to the request stream. Assuming you're doing all that, you should be able to catch the WebException that is raised. Have a look in the response stream that comes back from that and you might get some more information about what's going wrong. Chances are you're just passing in the wrong data for the parameters. I'll add a post soon with the code I had catching the requests to the APIs. Hope that helps

Comments have been disabled for this content.