3

Google アナリティクス レポート API を使用する PHP で内部アプリケーションを作成しています。アプリケーションは常に同じ Google アカウントを使用してアナリティクスに接続するため、データベースにアクセス トークンとリフレッシュ トークンだけを保持したいと考えています。

次のGoogle チュートリアルを使用して OAuth をセットアップしました。次の手順を実行しました。

ブートストラップ コード (すべてのステップ)

$client = new apiClient();
$client->setApplicationName("Stat-robot");
$client->setApprovalPrompt("force");            
$client->setAccessType('offline');
$client->setClientId("CLIENT_ID");
$client->setClientSecret("CLIENT_SECRET");
$client->setDeveloperKey("DEV_KEY");
$client->setRedirectUri("http://localhost/statistics/api");
$client->setScopes("https://www.googleapis.com/auth/analytics.readonly");

ステップ1

Redirect to the URL retrieved from $client->createAuthUrl())

ステップ2

//Get a redirect from Google with a GET-variable 'code'
$client->authenticate();
$accessToken = $client->getAccessToken
//This gives me a JSON-format access-token

ステップ 3

//Set the access token retrieved in previous request
$client->setAccessToken($accessToken);
//set the refresh token from the JSON response
$client->refreshToken($refreshToken);

ここで、最後の部分 ( refreshToken) でエラーが発生します: invalid_grant。SO とその他のインターネットで読んだことがありますが、アクセス タイプを に設定offlineし、承認プロンプトを に設定する必要がありますforce。しかし、私は持っていると思います(上記を見て)。

それで、何がうまくいかないのですか?

編集: Google API 自体のソースを読んでいました。sign()OAuth クラスのメソッドは、次を指定します。

if ($expired) {
    if (! array_key_exists('refresh_token', $this->accessToken)) {
        throw new apiAuthException("The OAuth 2.0 access token has expired, "
            . "and a refresh token is not available. Refresh tokens are not "
            . "returned for responses that were auto-approved.");
      }
      $this->refreshToken($this->accessToken['refresh_token']);
    }
}

だから、本当に電話する必要はありません$client->refreshToken()

4

2 に答える 2

0

受信したエラー"invalid_grant"を解決するには、サーバー クロックも更新するように Google にアドバイスしてください。API 呼び出しの最大数に達したかどうかを確認してください。

于 2014-05-12T14:14:41.890 に答える