0

我已经检查了 stackoverflow 并尝试了几种解决方案,但是在尝试向 google 令牌服务器发出发布请求以将代码换成 oauth2 令牌时,我继续得到 400。该代码正在从查询字符串中正确检索。我已经尝试过对秘密进行 URL 编码、重定向 URL 并将请求的编码更改为 ASCII。我知道 google 对标头很挑剔,但 json 响应故意含糊不清,只返回“错误:错误请求”我无法调试请求有什么问题。我还尝试在 chrome REST 客户端中测试发布请求并收到相同的错误。我假设标题中存在错误或其他格式问题,但谷歌没有返回有用的错误代码。

//用工作代码编辑

    //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;
    }
4

1 回答 1

0

在我看来,您的 urlencoding 可能搞砸了。您对 locovisual.com URL 进行 urlencode,但仅此而已。400 可靠地意味着您的请求在语法上有些混乱。

于 2013-05-01T22:22:23.317 回答