disqus API缺少必需参数:grant_type

时间:2012-06-26 11:18:25

标签: c# api url oauth-2.0 disqus

我正在尝试通过disqus API检索access_token。

这是我的网址:

https://disqus.com/api/oauth/2.0/access_token/?client_id=cvwNO7HaRwgYDq9anat8j7uzowJ8HBEz8gH7mUnmMhC0BKZZTkObc5d7o242liNG&grant_type=authorization_code&client_secret=Hrrgy1ZLcLN0qjmZhzXR2owET8cGazcbcGNxTlsWEJYiNfc3JcQLbKx2PYW6yNU7&redirect_uri=http://www.aftenposten.no&code=PM6QYwUJ

我收到错误:缺少必需参数:grant_type

我正在使用以下代码来获取响应:

HttpWebRequest request = HttpWebRequest.CreateHttp(uri);
request.Method = "POST";
request.BeginGetResponse(new AsyncCallback(getAccessTokenResponse), request);

1 个答案:

答案 0 :(得分:1)

您需要在请求正文中传递表单数据,而不是作为url参数传递。试一试:

var uri = "https://disqus.com/api/oauth/2.0/access_token/"
HttpWebRequest request = HttpWebRequest.CreateHttp(uri);
var data = "client_id=[your client id]"
data += "&grant_type=authorization_code"
data += "&client_secret=[your client secret]"
data += "&redirect_uri=http://www.aftenposten.no&code=PM6QYwUJ"
var postData = Encoding.ASCII.GetBytes(data);

request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.Length;

using (var stream = request.GetRequestStream())
{
    stream.Write(data, 0, data.Length);
}

var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();