刷新OAuth2令牌时出错,消息:'{“error”:“invalid_grant”}'

时间:2014-11-19 16:30:24

标签: laravel google-analytics oauth-2.0 google-api-php-client

我正在使用这个包Analytics-Laravel 4进行谷歌分析,我正确地跟随所有步骤。当我尝试获取网站ID时,我遇到了这个错误:

Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }'

我已经仔细检查了所有配置,客户端ID,service_account和私钥,但仍然出现错误。 我应该尝试检查的其他任何事情可能会解决这个问题吗?!

2 个答案:

答案 0 :(得分:1)

之前我没有使用此软件包,我使用google-api-php-client,但无论如何,如果您没有设置刷新令牌,则会出现此错误。

您应该知道只需要一次访问令牌。您还需要将访问类型设置为脱机,这将为您提供刷新令牌,您将使用该令牌自动获取新的访问令牌,而无需在每次访问令牌到期时获取新代码。

在Google的控制台中,我为网络应用程序创建了客户端ID 。确保将重定向URI设置为您将收到代码的网页,并使用该代码提取访问令牌。

以下是使用google-api-php-client的代码示例,我希望它会有所帮助:

您只需运行以下代码一次,并检索并存储访问令牌。

<?php

require_once('google-api-php-client-master/src/Google/Client.php');

session_start();

$client = new Google_Client();
$client->setApplicationName('APP_NAME');

$client->setClientId(YOUR_CLIENT_ID);
$client->setClientSecret('YOUR_CLIENT_SECRET');
$client->setRedirectUri('YOUR_REDIRECT_URI');
$client->setDeveloperKey('YOUR_DEV_KEY');
$client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));
$client->setAccessType("offline");

// Step 1: Create an auth url
if (isset($_GET['ref']) && $_GET['ref'] == "1") {
    $authUrl = $client->createAuthUrl();
    return Redirect::to($authUrl);
}   

// Step 2: The user accepted your access now you need to exchange it.
if (isset($_GET['code'])) {
    $client->authenticate($_SESSION['code']);  //Authenticate the client
    $token = $client->getAccessToken();  //Get the access token
    var_dump($token); //Store the token in your DB or config file
    die();
}

?>

从上面的代码中获取访问令牌(应该包含刷新令牌)后,将其存储在您的数据库或配置文件中。

现在,以下代码应通过 getAccessToken 函数验证客户端并在访问令牌到期时刷新访问令牌

<?php

require_once('google-api-php-client-master/src/Google/Client.php');
require_once('google-api-php-client-master/src/Google/Service/Analytics.php');

$client = new Google_Client();
$client->setApplicationName('APP_NAME');

$client->setClientId(YOUR_CLIENT_ID);
$client->setClientSecret('YOUR_CLIENT_SECRET');
$client->setRedirectUri('YOUR_REDIRECT_URI');
$client->setDeveloperKey('YOUR_DEV_KEY');
$client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));
$client->setAccessType("offline"); //Make sure the access type is offline to get a refresh token

$config = CoreConfig::find(1); //Getting the first record from the config table
$client->setAccessToken($config->google_access_token); //Retrieve the access token that you stored and set it to the client object

//Check this the token is expired
if($client->isAccessTokenExpired()) {
    $token = json_decode($config->google_access_token, true); //Get the token stored, and convert JSON to array
    $client->refreshToken($token['refresh_token']); //Set the refresh token
    $newtoken = $client->getAccessToken(); //Call the getAccessToken() function to get a new access token for you
    $config->update(array('google_access_token' => $newtoken)); //Store the new token in your DB
}

if ($client->getAccessToken()) {
    $analytics = new Google_Service_Analytics($client);
    //Do something with the $analytics object
}

?>

答案 1 :(得分:1)

可能是服务器时间。如果您服务器上的当地时间与Google的oAuth服务器不同步,即使只是几秒钟,您也会收到该错误消息。

您可以通过运行&#34; date&#34;来检查时间。在控制台中。

运行&#34; sudo ntpdate ntp.ubuntu.com&#34;为我们解决了。

相关问题