1
我使用Google API檢索分析數據並將其顯示在我的網站上。它好工作了一個月,但最近四天我得到這個錯誤:Google API客戶端刷新OAuth2令牌
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
'[email protected]',
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:[email protected]=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();}
?>