Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# twitter应用程序OAuth403_C#_.net_Twitter Oauth - Fatal编程技术网

C# twitter应用程序OAuth403

C# twitter应用程序OAuth403,c#,.net,twitter-oauth,C#,.net,Twitter Oauth,我正在尝试执行应用程序oauth,如下所述: 但我这样做的结果是403(禁止)我尝试的C#代码是: String key = ConfigurationManager.AppSettings["TwitterConsumerKey"]; String secret = ConfigurationManager.AppSettings["TwitterConsumerSecret"]; String bearerCredentials = Http

我正在尝试执行应用程序oauth,如下所述:

但我这样做的结果是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();

关于我做错了什么,有什么建议吗?

把这个问题留给别人,以防别人犯我犯过的同样愚蠢的错误,把它写下来,告诉我这行的答案

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";

干得好!这对我帮助很大。