0

stackoverflow を確認し、いくつかの解決策を試しましたが、コードを oauth2 トークンと交換するために Google トークン サーバーにポスト リクエストを送信しようとすると、引き続き 400 が返されます。コードはクエリ文字列から適切に取得しています。シークレット、リダイレクト URL を URL エンコードし、リクエストのエンコードを ASCII に変更しようとしました。Google がヘッダーに気を遣っていることは知っていますが、json の応答は意図的にあいまいで、「エラー: 不正な要求」しか返されません。要求の何が問題なのかをデバッグできません。また、chrome REST クライアントで投稿リクエストをテストしようとしたところ、同じエラーが発生しました。ヘッダーにエラーがあるか、その他のフォーマットの問題があると思いますが、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;
    }
4

1 に答える 1

0

あなたの urlencoding が台無しになっているように見えます。locovisual.com の URL を urlencode しますが、それ以外は何もしません。400 は、リクエストに構文上の問題があることを確実に意味します。

于 2013-05-01T22:22:23.317 に答える