2015-10-11 271 views
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();} 
?> 

回答

0

問題解決了!

sudo ntpdate 3.pool.ntp.org 

你會發現從 public NTP time server list時間服務器的列表:

它是由服務器的時鐘引起的,我只是一個NTP服務器使用下面的命令(Ubuntu的在我的情況)進行同步

希望有幫助!

相關問題