C# .net twitter api缺少必需参数:授权类型:禁止\u缺少\u参数
只需使用c#.net程序获取Twitter API的访问令牌。我将http post请求组合如下(省去catch语句以节省空间): Twitter api正在发回响应:C# .net twitter api缺少必需参数:授权类型:禁止\u缺少\u参数,c#,.net,http,httpwebrequest,httpwebresponse,C#,.net,Http,Httpwebrequest,Httpwebresponse,只需使用c#.net程序获取Twitter API的访问令牌。我将http post请求组合如下(省去catch语句以节省空间): Twitter api正在发回响应: {“errors”:[{“code”:“170”,“message”:“缺少必需参数:grant_type”,“label”:“probled_Missing_parameter}]} 我理解错误的含义。它告诉我,我没有包括内容正文grant\u type=client\u credentials。但正如您所看到的,我正在使用S
{“errors”:[{“code”:“170”,“message”:“缺少必需参数:grant_type”,“label”:“probled_Missing_parameter}]}
我理解错误的含义。它告诉我,我没有包括内容正文grant\u type=client\u credentials
。但正如您所看到的,我正在使用Stream.Write
方法发送该内容正文
起初,我认为这是因为字符串不是URL编码的,可能它在等号之前需要一个反斜杠什么的。所以我URL编码字符串只是为了确保。我想它一定是以错误的格式,但我不确定如何
为什么Twitter api没有得到我发送给他们的内容主体
更新:我接受了@Linvi的建议,并在上复制了文章的格式。以下是我当前的代码:
byte[] buffer = ASCIIEncoding.ASCII.GetBytes(HttpUtility.UrlEncode("grant_type=client_credentials"));
var webReq = (HttpWebRequest)WebRequest.Create("https://api.twitter.com/oauth2/token");
webReq.Method = "POST";
webReq.Host = "api.twitter.com";
webReq.Headers.Set("Authorization", "Basic " + credentialBase64);
webReq.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
webReq.ContentLength = buffer.Length;
var reqStream = webReq.GetRequestStream();
reqStream.Write(buffer, 0, buffer.Length);
reqStream.Close();
var webResp = (HttpWebResponse)webReq.GetResponse();
我仍然收到一个格式为“403”的响应:{“errors”:[{“code”:“170”,“message”:“缺少必需的参数:grant\u type”,“label”:“probled\u Missing\u parameter}]}
我已经用一个本地程序测试了它,该程序查看内容体,结果是空的。我不明白为什么我的内容主体没有被编写 如果您确实共享了更多信息,但关于
授权类型
,如果您想使用仅限应用程序的凭据(仅使用使用者密钥和使用者机密),则需要此信息
在这种情况下,您需要将其添加到HttpRequestMessage
(HttpClient)中:
对于旧的HttpWebRequest,请使用:
byte[] requestBytes = new ASCIIEncoding().GetBytes(inputData);
//get the request stream to write the post to
Stream requestStream = request.GetRequestStream();
//write the post to the request stream
requestStream.Write(requestBytes, 0, requestBytes.Length);
来源:终于让它开始工作了。我必须停止对授权类型请求进行URL编码。对于试图从Twitter api获取访问令牌的其他人,以下是有效的代码:
byte[] buffer = ASCIIEncoding.ASCII.GetBytes("grant_type=client_credentials");
var webReq = (HttpWebRequest)WebRequest.Create("https://api.twitter.com/oauth2/token");
webReq.Method = "POST";
webReq.Host = "api.twitter.com";
webReq.Headers.Set("Authorization", "Basic " + credentialBase64);
webReq.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
webReq.ContentLength = buffer.Length;
var reqStream = webReq.GetRequestStream();
reqStream.Write(buffer, 0, buffer.Length);
reqStream.Close();
var webResp = (HttpWebResponse)webReq.GetResponse();
您列出的内容是我认为可以使用的,但HttpWebRequest没有内容属性。如果我尝试包含您的行,则会出现错误“HttpWebRequest不包含“内容”的定义,
Microsoft.Net.HttpClient
就是这种情况。我现在没有时间给您提供解决方案的HttpWebRequest
版本,但我会的。要设置HttpWebRequest
的内容,请查看此处是否有人对为什么在那里放置-1的评论?当您第一次给我答案时,它使用了HttpWebRequest不存在的属性。我知道现在您使用的是HttpClient的注释,这很好,但我无法更改它。对答案做一些修改,我就可以投-1票了。可能是重复的
byte[] requestBytes = new ASCIIEncoding().GetBytes(inputData);
//get the request stream to write the post to
Stream requestStream = request.GetRequestStream();
//write the post to the request stream
requestStream.Write(requestBytes, 0, requestBytes.Length);
byte[] buffer = ASCIIEncoding.ASCII.GetBytes("grant_type=client_credentials");
var webReq = (HttpWebRequest)WebRequest.Create("https://api.twitter.com/oauth2/token");
webReq.Method = "POST";
webReq.Host = "api.twitter.com";
webReq.Headers.Set("Authorization", "Basic " + credentialBase64);
webReq.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
webReq.ContentLength = buffer.Length;
var reqStream = webReq.GetRequestStream();
reqStream.Write(buffer, 0, buffer.Length);
reqStream.Close();
var webResp = (HttpWebResponse)webReq.GetResponse();