I'm working with Windows Live SDK from C# trying to invoke directly the rest service for getting my oAuth token, but instead I'm getting an 401 Error. Unauthorized.
I have my application registered as Desktop Application on https://manage.dev.live.com/ and published so I have my own Client ID and Secret Key.
From my desktop application I open an Internet Explorer Window for Live Login and get my Verification Code.
With that code, secret key and clientID I invoke AccessToken.aspx on this way:
try
{
string requestUrl = "https://consent.live.com/AccessToken.aspx";
// Request the access token.
string postData = string.Format("wrap_client_id={0}&wrap_client_secret={1}&wrap_verification_code={2}",
clientId,
secretKey,
verificationCode);
postData = HttpUtility.UrlEncode(postData);
byte[] postDataEncoded = System.Text.Encoding.UTF8.GetBytes(postData);
WebRequest req = HttpWebRequest.Create(requestUrl);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = postDataEncoded.Length;
Stream requestStream = req.GetRequestStream();
requestStream.Write(postDataEncoded, 0, postDataEncoded.Length);
WebResponse res = req.GetResponse();
string responseBody = null;
using (StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8))
{
responseBody = sr.ReadToEnd();
}
// Process FORM POST.
NameValueCollection responseCollection = System.Web.HttpUtility.ParseQueryString(responseBody);
return responseCollection["wrap_access_token"];
}
catch (Exception ex)
{
throw ex;
}
What I'm doing wrong?
Thanks in advance for any help provided.
The remote server returned an error: (401) Unauthorized