2013-02-27 65 views
0

我已經在這個問題現在已經2天,並試圖尋找谷歌代碼和計算器,但仍然可以拿出一個答案。谷歌API的分析無效的授權,但有時

我的問題是,當我嘗試我的谷歌Analytics API的我得到「錯誤刷新的OAuth2令牌,消息:‘{‘錯誤’:‘invalid_grant’}’」

但奇怪的是,有些時候它將工作。很少,但如果我刷新並繼續嘗試它,它會輸出。

我能找到的唯一的事情是,它可能是the refresh token limit has been exceeded

附件是我的代碼,如果有人可以幫助我或我指向正確的方向。

謝謝!

<?php 
session_start(); 
require_once 'Google_Client.php'; 
require_once 'Google_AnalyticsService.php'; 
require_once 'config.php'; 


$keyFile = 'key.p12'; 


$client = new Google_Client(); 
$client->setApplicationName("test"); 
if (isset($_SESSION['token'])) { 
    $client->setAccessToken($_SESSION['token']); 
    } 
$client->setAssertionCredentials(new Google_AssertionCredentials(
GOOGLE_SERVICE_ACCOUNT, 
array('https://www.googleapis.com/auth/analytics.readonly'), 
file_get_contents($keyFile)) 
); 
$client->setClientId(GOOGLE_CLIENT_ID); 
$client->setAccessType('offline'); 
$client->setUseObjects(true); 
$service = new Google_AnalyticsService($client); 
try { 
    $results = $service->data_ga->get(
    'ga:44444444', 
    date('Y-m-d', strtotime('-30 days '.date('Y-m-d', strtotime('-1 day '.date('Y-m- d'))))), 
    date('Y-m-d', strtotime('-1 day '.date('Y-m-d'))), 
    'ga:visits,ga:newVisits', 
    /*array(
     'dimensions' => 'ga:source,ga:keyword', 
     'sort' => '-ga:visits,ga:keyword', 
     'filters' => 'ga:medium==organic', 
     'max-results' => '25' 
    )*/ 
    array('dimensions' => 'ga:date') 
); 
} catch (Google_ServiceException $e) { 
// echo $e->getMessage(); 
    } 
if ($client->getAccessToken()) { 
    $_SESSION['token'] = $client->getAccessToken(); 
    } 

    $dateParsePattern = '/"Date.parse\(\\\"((\d{4})-(\d{2})-(\d{2})) UTC\\\"\)"/'; 
    $dateParseReplacement = 'Date.parse("$1 UTC")'; 
    $allVisitsItems = array(); 
    $newVisitorsItems = array(); 
if ($results && count($results->getRows()) > 0) { 
    foreach ($results->getRows() as $row) { 
    $date = 'Date.parse("'.date("Y-m-d", strtotime($row[0])).' UTC")'; 
    $allVisitsItems[] = array($date, intval(htmlspecialchars($row[1], ENT_NOQUOTES))); 
    $newVisitorsItems[] = array($date, intval(htmlspecialchars($row[2], ENT_NOQUOTES))); 
} 
} 
    header('Content-Type: application/json'); 
?> 

<?php echo preg_replace($dateParsePattern, $dateParseReplacement, json_encode($allVisitsItems)) ?> 

編輯 - 這不是NTP,當我附和日期( 'F年小時l的JS \:I:S A');它匹配起來。

回答

0

以下工作可以生成輸出 - 確保您運行的是PHP 5.3或更高版本。

<?php 
require_once './src/Google_Client.php'; 
require_once './src/contrib/Google_AnalyticsService.php'; 

$path_to_keyfile = '.p12'; 
$service_account_email = '[email protected]'; 
$client_id = '7.apps.googleusercontent.com'; 
$analytics_profile_id = 'ga:IN URL OF ANALYTICS'; 

$client = new Google_Client(); 
$client->setApplicationName("API TEST"); 
$client->setAssertionCredentials(
new Google_AssertionCredentials(
    $service_account_email, 
    array('https://www.googleapis.com/auth/analytics.readonly'), 
    file_get_contents($path_to_keyfile) 
) 
); 
$client->setClientId($client_id); 
$client->setAccessType('offline_access'); 

$service = new Google_AnalyticsService($client); 

$from = date('Y-m-d', strtotime('-30 days '.date('Y-m-d', strtotime('-1 day '.date('Y-m-d'))))); // 30 days 
$to = date('Y-m-d', strtotime('-1 day '.date('Y-m-d'))); // yesterday 

    $dateParsePattern = '/"Date.parse\(\\\"((\d{4})-(\d{2})-(\d{2})) UTC\\\"\)"/'; 
    $dateParseReplacement = 'Date.parse("$1 UTC")'; 
    $allVisitsItems = array(); 
    $newVisitorsItems = array(); 

try { 
$data = $service->data_ga->get(
    // 
    $analytics_profile_id, 
    $from, 
    $to, 
    'ga:visits,ga:newVisits', 
    array('dimensions' => 'ga:date') 
); 

if($data && $data['totalResults'] > 0) { 
    foreach($data['rows'] as $row) { 
     $date = 'Date.parse("'.date("Y-m-d", strtotime($row[0])).' UTC")'; 
     $allVisitsItems[] = array($date, intval(htmlspecialchars($row[1], ENT_NOQUOTES))); 
     $newVisitorsItems[] = array($date, intval(htmlspecialchars($row[2], ENT_NOQUOTES))); 
    } 
} 

header('Content-Type: application/json'); 
?> 

<?php echo preg_replace($dateParsePattern, $dateParseReplacement, json_encode($allVisitsItems)) ?> 

<?php echo preg_replace($dateParsePattern, $dateParseReplacement, json_encode($newVisitorsItems)) ?> 

<?php 
} catch(Exception $e) { 
echo 'There was an error : - ' . $e->getMessage(); 
}