2016-09-30 53 views
0

我一直在尋找並查找這個爲期兩天的時間,並且無法弄清楚我哪裏出錯了。我試圖從我的谷歌分析帳戶使用PHP拉數據。我已按照所有步驟設置了服務帳戶並下載了JSON文件。這裏是我使用的代碼:使用PHP和服務帳戶的Google AnalyticsAPI

// Load the Google API PHP Client Library 
require_once ('/google-api-php-client-1-master/src/Google/autoload.php'); 
$KEY_FILE_LOCATION = '/inc/ac_key.json'; 
// Create and configure a new client object. 
$client = new Google_Client(); 
$client->setApplicationName("Hello Analytics Reporting"); 
$client->setAuthConfig($KEY_FILE_LOCATION); 
$client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']); 
$analytics = new Google_Service_Analytics($client); 
function getResults($analytics, $profileId) { 
    // Calls the Core Reporting API and queries for the number of sessions 
    // for the last seven days. 
    return $analytics->data_ga->get(
     'ga:' . $profileId, 
     '30daysAgo', 
     'today', 
     'ga:pageviews' 
    ); 
} 
$profile = $r['google_analytics']; 
$results = getResults($analytics, $profile); 
$month = date('n'); 
$year = date('Y'); 
$results->setMonth($month,$year); 
$visits = $results->getVisitors(); 
$views = $results->getPageviews(); 
/* build tables */ 
if(count($visits)) { 
    foreach($visits as $day=>$visit) { 
     $flot_datas_visits[] = '['.$day.','.$visit.']'; 
     $flot_datas_views[] = '['.$day.','.$views[$day].']'; 
    } 
    $flot_data_visits = '['.implode(',',$flot_datas_visits).']'; 
    $flot_data_views = '['.implode(',',$flot_datas_views).']'; 
} 

我正在一個錯誤的無效client_secret.json文件,但我試圖使用一個服務帳戶,所以我不知道是怎麼回事進行身份驗證。然後我試圖將數據繪製在flot表中,但我並不擔心這一部分,因爲我仍然試圖通過第一個障礙。任何幫助,將不勝感激!

+0

,並且使用的是這裏哪個庫? –

+0

致命錯誤:未收到的異常'Google_Exception'帶消息'無效的客戶機密鑰JSON文件'。 in /home/acclients/google-api-php-client-1-master/src/Google/Client.php:171堆棧跟蹤:#0 /public_html/dashboard.php(27):Google_Client-> setAuthConfig('/ home/acclient/...')#1 {main}扔在/google-api-php-client-1-master/src/Google/Client.php 171行上 該庫是代碼的第一行發佈在我的問題 –

+0

你確定存在'/ inc/ac_key.json'嗎? –

回答

0

我對谷歌PHP API客戶端不是最新的,但顯然有不同的版本 - 當我克隆回購時,我得到了你應用使用的版本,但我沒有得到它使用我的憑證文件。然後,我通過作曲家安裝庫的當前版本:

composer require google/apiclient:^2.0 

當時我能夠用下面的代碼來訪問谷歌分析:

<?php 
include_once __DIR__ . '/vendor/autoload.php'; 
date_default_timezone_set('America/Los_Angeles'); 
$client = new Google_Client(); 

$client->setAuthConfig('/path/to/credentials.json'); 

$client->setApplicationName("Client_Library_Examples"); 
$client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']); 
$service = new Google_Service_Analytics($client); 

function getResults($service, $profileId) { 
    return $service->data_ga->get(
     'ga:' . $profileId, 
     '30daysAgo', 
     'today', 
     'ga:pageviews' 
    ); 
} 

$profile= "80493610"; 
$results = getResults($service, $profile); 

print_r($results); 

有了一點閒逛的我managend得到在您的示例中使用舊版本的分析服務實例。顯然,你有你能告訴你是從的代碼行獲取該錯誤究竟使用loadServiceAccountJson功能,而不是setAuthConfig

<?php 

// Load the Google API PHP Client Library 
require_once ('google-api-php-client/src/Google/autoload.php'); 
$KEY_FILE_LOCATION = '/path/to/credentials.json'; 
// Create and configure a new client object. 
$client = new Google_Client(); 
$client->setApplicationName("Hello Analytics Reporting"); 
$scopes = ['https://www.googleapis.com/auth/analytics.readonly']; 
$client->loadServiceAccountJson($KEY_FILE_LOCATION, $scopes); 

$analytics = new Google_Service_Analytics($client); 

print_r($analytics); 
+0

完成了潛水並安裝了作曲家,以便按預期工作。謝謝! –

相關問題