【问题标题】:Trading Google Oauth2 Code for Oauth2 Token in Asp .Net在 Asp .Net 中用 Google Oauth2 代码换取 Oauth2 令牌
【发布时间】:2013-03-20 20:07:54
【问题描述】:

我已经检查了 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;
    }

【问题讨论】:

    标签: asp.net oauth-2.0 token google-oauth


    【解决方案1】:

    在我看来,您的 urlencoding 可能搞砸了。您对 locovisual.com URL 进行 urlencode,但仅此而已。 400 可靠地表示您的请求在语法上存在问题。

    【讨论】:

      猜你喜欢
      • 2016-10-13
      • 2015-04-09
      • 2014-02-04
      • 1970-01-01
      • 2020-05-08
      • 2013-12-27
      • 2012-07-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多