2013-06-18 67 views
1

我試圖爲概述這裏做應用程序的OAuth:https://dev.twitter.com/docs/api/1.1/post/oauth2/tokenTwitter應用程序的OAuth 403

,但我嘗試這樣做導致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(); 

任何關於我在這裏做錯的建議?

回答

2

的情況下,其他人徹底告別了這個問題作出了同樣愚蠢的錯誤,我做到了,寫起來指給我答案此行

有一個不必要的Content-Type被設置應該是

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

太棒了!這幫了我很多。 –

相關問題