1

我正在尝试按照此处所述进行应用程序 oauth:https ://dev.twitter.com/docs/api/1.1/post/oauth2/token

但我这样做的尝试导致 403(forbidden) 我正在尝试的 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();

关于我在这里做错了什么有什么建议吗?

4

1 回答 1

3

留下这个问题以防其他人犯我犯的同样愚蠢的错误,写下来向我指出这一行的答案

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";
于 2013-06-18T14:30:13.250 回答