1

OAuth2 を使用して Google アナリティクス API アプリケーションのオフライン アクセスを設定しようとしています。ASP.NET を使用して更新トークンと引き換えに認証コードを POST していますが、サーバーからの応答を取得できません。リフレッシュトークン。

ポスト リクエストに MSDN ドキュメントのサンプル コードを使用したので、これが正しいとしか思えませんが、「リモート サーバーがエラーを返しました: (400) System.Net.HttpWebRequest での不正なリクエストです。 GetResponse()":

using System;
using System.IO;
using System.Net;
using System.Text;

WebRequest request = WebRequest.Create ("https://accounts.google.com/o/oauth2/token?code=xxxmyauthorizationcodexxx&client_id=xxxxxxxxx.apps.googleusercontent.com&client_secret=xxxxxxxxxxxx&redirect_uri=https://mysite.com/oauth2callback&grant_type=authorization_code");
request.Method = "POST";
string postData = "code=xxxmyauthorizationcodexxx&client_id=xxxxxxxxx.apps.googleusercontent.com&client_secret=xxxxxxxxxxxx&redirect_uri=https://mysite.com/oauth2callback&grant_type=authorization_code";
byte[] byteArray = Encoding.UTF8.GetBytes (postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;

Stream dataStream = request.GetRequestStream ();
dataStream.Write (byteArray, 0, byteArray.Length);
dataStream.Close ();

WebResponse response = request.GetResponse ();
dataStream = response.GetResponseStream ();
StreamReader reader = new StreamReader (dataStream);
string responseFromServer = reader.ReadToEnd ();

reader.Close ();
dataStream.Close ();
response.Close ();

GET メソッドを使用して認証コードを取得することに成功しました。このドキュメントに従っています: https://developers.google.com/accounts/docs/OAuth2WebServer#handlingtheresponse

また、https Web サイトを使用して要求を行っており、認証コードの有効期限が切れたときに手動で更新しています。誰でもこの問題の解決策を持っていますか?

編集: 同じ問題が発生している場合は、まず以下の aeijdenberg の応答を見てください。ただし、私の解決策は、コード パラメーターに使用した認証コードがすぐに期限切れになることでした。新しいページを要求せずにページを更新し続けました。データを取得するには、responseFromServer 変数からコンテンツを表示するだけです。

4

1 に答える 1