2013-03-20 60 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