我正在使用PHP创建一个内部应用程序,该应用程序使用Google Analytics报告API。该应用程序将始终使用相同的Google帐户连接到Analytics,因此我希望只在我的数据库中保留访问和刷新令牌。
我使用了 Google教程来设置OAuth。我采取了以下步骤: 引导代码(每个步骤都需要)
步骤2。
步骤3
现在,最后一部分(
那么出了什么问题? 编辑:我正在阅读Google API本身的源代码。 OAuth类中的
我使用了 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。
我在Stack Overflow和其他互联网上读到,我应该将我的访问类型设置为offline
,并将我的批准提示设置为force
。但是我认为我已经这样做了(看上面的内容)。那么出了什么问题? 编辑:我正在阅读Google API本身的源代码。 OAuth类中的
sign()
方法指定如下: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()
access_type=offline
。 我想我已经这样做了。 - Bart VangeneugdengetAccessToken()
的响应JSON中获取和存储刷新令牌。 - Jan Gerlinger