我使用Google API来检索分析数据并在我的网站上显示它们。它工作了一个月,但最后四天我得到这个错误:
Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }'
代码如下:
<?php
require_once ('src/Google/autoload.php');
$client = new Google_Client();
$client->setApplicationName( 'GAFetchData' );
$client->setAssertionCredentials(
new Google_Auth_AssertionCredentials(
// Google client ID email address
'xxxxx-xxxxxxxxxx@developer.gserviceaccount.com',
array('https://www.googleapis.com/auth/analytics.readonly'),
file_get_contents( $Path.'gapii/'.'GAFetchData-xxxxxxxx.p12' )
)
);
$client->setClientId( 'xxxxx-xxxxxxxxxx.apps.googleusercontent.com' );
$client->setAccessType( 'offline_access' );
$analytics = new Google_Service_Analytics( $client );
$analytics_id = 'ga:xxxxxx';
try {
$optParams = array();
$metrics = 'ga:pageviews';
$start_date = '7daysAgo';
$end_date = 'today';
$optParams['dimensions'] = 'ga:pagePath';
$optParams['filters'] = 'ga:pagePath!@=rech;';
$optParams['max-results'] = '10000';
$result = $analytics->data_ga->get( $analytics_id, $start_date, $end_date, $metrics, $optParams);
if( $result->getRows() ) { $items = $result->getRows(); }
}
catch(Exception $e) { echo $e->getMessage();}
?>
问题解决!
这是由服务器的时钟引起的,我只是使用以下命令(在我的情况下是Ubuntu)将它与NTP服务器同步:
sudo ntpdate 3.pool.ntp.org
中的时间服务器列表公共NTP时间服务器列表