2013-03-20 62 views
0

我已經檢查計算器和嘗試了幾種解決方案,但我仍然得到400試圖使一個POST請求谷歌令牌服務器,交易代碼爲的oauth2令牌時。該代碼正在從查詢字符串中正確檢索。我已嘗試使用URL編碼祕密,重定向URL並將請求上的編碼更改爲ASCII。我知道,谷歌是用挑剔的頭,但JSON響應是故意含糊不清,僅返回「錯誤:錯誤的請求」我不能調試什麼是錯的請求。我也嘗試在Chrome REST客戶端中測試發佈請求並收到相同的錯誤。我認爲在頭文件中有一個錯誤或者其他一些格式問題,但是Google沒有返回有用的錯誤代碼。交易谷歌的oauth2代碼的OAuth2令牌中的ASP.NET

//編輯一起工作代碼

//trade code for token 
    public static String CodeTrade(String code) 
    { 
     String apiResponse; 

      string codeClient = "code=" + code + "&client_id=xxx.apps.googleusercontent.com&"; 
      string secretUri = "client_secret=zzz&grant_type=authorization_code&redirect_uri=" + "https://web.locusvisual.com/gadgets/smallview/loginTrue.aspx"; 
      string postData = codeClient + secretUri; 
      // Create a request using a URL that can receive a post. 
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://accounts.google.com/o/oauth2/token"); 

      // Set the Method property of the request to POST. 
      request.Method = "POST"; 
      // Set the ContentType property of the WebRequest. 
      request.ContentType = "application/x-www-form-urlencoded"; 


      // Create POST data and convert it to a byte array. 
      byte[] byteArray = Encoding.UTF8.GetBytes(postData); 

       // Get the request stream. 
       Stream dataStream = request.GetRequestStream(); 

       // Write the data to the request stream. 
       dataStream.Write(byteArray, 0, byteArray.Length); 

       // Close the Stream object. 
       dataStream.Close(); 

       // Get the response. 
       WebResponse response = request.GetResponse(); 

       // Display the status. 
       apiResponse = ((HttpWebResponse)response).StatusDescription.ToString(); 
       //Console.WriteLine(((HttpWebResponse)response).StatusDescription); 

       // Get the stream containing content returned by the server. 
       dataStream = response.GetResponseStream(); 

       // Open the stream using a StreamReader for easy access. 
       StreamReader reader = new StreamReader(dataStream); 

       // Read the content. 
       string responseFromServer = reader.ReadToEnd(); 
       // Display the content. 
       Console.WriteLine(responseFromServer); 
       // Console.ReadKey(); 
       // Clean up the streams. 



       apiResponse = responseFromServer; 
       reader.Close(); 
       dataStream.Close(); 
       response.Close(); 

     return apiResponse; 
    } 

回答

0

我看來像你的URL編碼可能會搞砸。您urlencode locovisual.com網址,但沒有別的。 400可靠意味着語法上有一些東西與您的請求混淆。

+0

是的!網址編碼是問題!謝謝回覆! – TauterTwiggy 2013-05-03 18:31:31