当我尝试从live.com获取访问令牌时,也会返回错误请求(400)

时间:2016-03-15 14:11:07

标签: c# oauth-2.0

我已经获得了授权码。 我尝试使用以下代码获取访问令牌:

string requestUrl = "https://login.live.com/oauth20_token.srf";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUrl);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
string fields = string.Format("client_id={0}&redirect_uri={1}&client_secret={2}&code={3}&grant_type=authorization_code", clientId_, "https://login.live.com/oauth20_desktop.srf", clientSecret_, authorizationCode_);
var data = Encoding.ASCII.GetBytes(fields);
request.ContentLength = data.Length;
using (Stream stream = request.GetRequestStream())
{
    stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();

最后一个字符串总是引发WebException:Bad Request

更新:只需从字段中删除client_secret并获取访问令牌:

string fields = string.Format("client_id={0}&redirect_uri={1}&code={2}&grant_type=authorization_code", clientId_, "https://login.live.com/oauth20_desktop.srf", authorizationCode_);

0 个答案:

没有答案