Twitter应用程序oauth 403

本文关键字:oauth 应用程序 Twitter | 更新日期: 2023-09-27 18:18:58

我正在尝试执行应用程序验证,如下所示: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();

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

Twitter应用程序oauth 403

留下这个问题,以防别人犯和我一样愚蠢的错误,把它写下来给我指出了这一行的答案

request.ContentType = "Content-Type: application/x-www-form-urlencoded;charset=UTF-8";
            string postData = "grant_type=client_credentials";

有一个不必要的内容类型设置,它应该是

request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
            string postData = "grant_type=client_credentials";