在Asp.Net中用Google Oauth2代码交换Oauth2令牌

在Asp.Net中用Google Oauth2代码交换Oauth2令牌,asp.net,oauth-2.0,token,google-oauth,Asp.net,Oauth 2.0,Token,Google Oauth,我已经检查了stackoverflow,并尝试了几种解决方案,但当我试图向google令牌服务器发出post请求,用代码交换oauth2令牌时,我仍然得到了400分。代码正在从查询字符串中正确检索。我尝试了URL编码的秘密,重定向URL和改变编码的要求,以ASCII。我知道google对标题很挑剔,但json响应故意含糊不清,只返回“error:Bad Request”,我无法调试请求的错误。我还尝试在chrome REST客户端中测试post请求,并收到相同的错误。我假设标题中存在错误或其他格

我已经检查了stackoverflow,并尝试了几种解决方案,但当我试图向google令牌服务器发出post请求,用代码交换oauth2令牌时,我仍然得到了400分。代码正在从查询字符串中正确检索。我尝试了URL编码的秘密,重定向URL和改变编码的要求,以ASCII。我知道google对标题很挑剔,但json响应故意含糊不清,只返回“error:Bad Request”,我无法调试请求的错误。我还尝试在chrome REST客户端中测试post请求,并收到相同的错误。我假设标题中存在错误或其他格式问题,但google没有返回有用的错误代码

//用工作代码编辑

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

在我看来,你的URL编码可能会搞砸。您可以对locovisial.com URL进行URL编码,但不编码其他内容。400表示您的请求在语法上有问题