我正在尝试执行此处概述的应用程序oauth:https://dev.twitter.com/docs/api/1.1/post/oauth2/token
但我尝试这样做导致403(禁止)我正在尝试的C#代码是:
String key = ConfigurationManager.AppSettings["TwitterConsumerKey"];
String secret = ConfigurationManager.AppSettings["TwitterConsumerSecret"];
String bearerCredentials = HttpUtility.UrlEncode(key) + ":" + HttpUtility.UrlEncode(secret);
bearerCredentials = Convert.ToBase64String(Encoding.UTF8.GetBytes(bearerCredentials));
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.twitter.com/oauth2/token");
request.Method = "POST";
request.Headers.Add("Authorization", "Basic " + bearerCredentials);
request.ContentType = "Content-Type: application/x-www-form-urlencoded;charset=UTF-8";
string postData = "grant_type=client_credentials";
byte[] data = System.Text.Encoding.UTF8.GetBytes(postData);
request.ContentLength = data.Length;
request.GetRequestStream().Write(data, 0, data.Length);
WebResponse response = request.GetResponse();
对我在这里做错了什么建议?
答案 0 :(得分:2)
如果其他人犯了同样的愚蠢错误我就把这个问题留了下来,写下来就向我指出了这句话的答案
request.ContentType = "Content-Type: application/x-www-form-urlencoded;charset=UTF-8";
string postData = "grant_type=client_credentials";
设置了一个不必要的Content-Type,它应该是
request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
string postData = "grant_type=client_credentials";