[英]The remote server returned an error: (401) Unauthorized
I'm trying to get the access token from the windows live connect api by using this code 我正在尝试通过使用此代码从Windows Live Connect API获取访问令牌
string requestUrl = "https://consent.live.com/AccessToken.aspx"; 字符串requestUrl =“ https://consent.live.com/AccessToken.aspx”;
// Request the access token.
string postData = string.Format("{0}?wrap_client_id={1}&wrap_client_secret={2}&wrap_callback={3}&wrap_verification_code={4}&idtype={5}",
requestUrl,
"000000004C039809",
"l4VJekL1vFL1iFVmcP5qLkWv9ukY4mdl",
"http://ewshops.com",
"dac5d71d-d640-30d1-ebed-3576b132b3ec",
"cid");
byte[] postDataEncoded = System.Text.Encoding.UTF8.GetBytes(postData);
WebRequest req = HttpWebRequest.Create(requestUrl);
req.Method = "POST";
// req.
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"];
but I've recieved the following error 但我收到以下错误
The remote server returned an error: (401) Unauthorized. 远程服务器返回错误:(401)未经授权。
Show us the response body, it usually contains more information. 向我们显示响应正文,它通常包含更多信息。 You should also urlencode
http://ewshops.com
before adding it to the uri. 在将其添加到uri之前,您还应该urlencode
http://ewshops.com
。
I had the same problem and fixed it as follows. 我遇到了同样的问题,并按以下步骤进行修复。 Remove the requestUrl ("https://consent.live.com/AccessToken.aspx") and the subsequent "?"
删除requestUrl(“ https://consent.live.com/AccessToken.aspx”)和后续的“?” from your postData.
从您的postData中。 The POST data should be in x-www-form-urlencoded format and that does not include the request URL.
POST数据应采用x-www-form-urlencoded格式,并且不包含请求URL。 Also HttpUtility.UrlEncode() all the parameters.
也是HttpUtility.UrlEncode()的所有参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.