4

我想通過背景cron作業將數據從谷歌分析引入到自己的數據庫中,而用戶不必每次都進行身份驗證。如何從服務器端後臺服務訪問Google AnalyticsAPI?

我知道how to get an Google Analytics OAuth Access Token with user interaction,正如我以前問。使用OAuth將不起作用,因爲它需要用戶交互。根據Google Analytics API Reference OAuth和訪問令牌可用於訪問每個會話基礎的頁面統計信息。不過,我正在尋找一種持久的方式來在後臺服務中實現這一點。

如何在沒有用戶身份驗證的情況下訪問Google Analytics或獲取無效訪問令牌?

回答

4

假設有問題的帳戶是您自己的,您可以使用service account。確保您將帳戶級別的服務帳戶電子郵件地址讀取權限授予Google Analytics帳戶,並且可以讀取您的數據。

<?php 
session_start(); 
require_once 'Google/Client.php'; 
require_once 'Google/Service/Analytics.php'; 

/************************************************ 
    The following 3 values an befound in the setting 
    for the application you created on Google 
    Developers console. 
    The Key file should be placed in a location 
    that is not accessable from the web. outside of 
    web root. 

    In order to access your GA account you must 
    Add the Email address as a user at the 
    ACCOUNT Level in the GA admin. 
************************************************/ 
$client_id = '1046123799103-nk421gjc2v8mlr2qnmmqaak04ntb1dbp.apps.googleusercontent.com'; 
$Email_address = '[email protected]eaccount.com'; 
$key_file_location = '629751513db09cd21a941399389f33e5abd633c9-privatekey.p12'; 

$client = new Google_Client(); 
$client->setApplicationName("Client_Library_Examples"); 

$key = file_get_contents($key_file_location); 

// seproate additional scopes with a comma 
$scopes ="https://www.googleapis.com/auth/analytics.readonly"; 

$cred = new Google_Auth_AssertionCredentials(
    $Email_address, 
    array($scopes), 
    $key 
    ); 

$client->setAssertionCredentials($cred); 
if($client->getAuth()->isAccessTokenExpired()) { 
    $client->getAuth()->refreshTokenWithAssertion($cred); 
} 

$service = new Google_Service_Analytics($client); 
$accounts = $service->management_accountSummaries->listManagementAccountSummaries(); 

//calulating start date 
$date = new DateTime(date("Y-m-d")); 
$date->sub(new DateInterval('P10D')); 

//Adding Dimensions 
$params = array('dimensions' => 'ga:userType'); 
// requesting the data 
$data = $service->data_ga->get("ga:78110423", $date->format('Y-m-d'), date("Y-m-d"), "ga:users,ga:sessions", $params); 


?><html> 
<?php echo $date->format('Y-m-d') . " - ".date("Y-m-d"). "\n";?> 
<table> 
<tr> 
<?php 
//Printing column headers 
foreach($data->getColumnHeaders() as $header){ 
    print "<td>".$header['name']."</td>"; 
} 
?> 
</tr> 
<?php 
//printing each row. 
foreach ($data->getRows() as $row) {  
    print "<tr><td>".$row[0]."</td><td>".$row[1]."</td><td>".$row[2]."</td></tr>"; 
} 

//printing the total number of rows 
?> 
<tr><td colspan="2">Rows Returned <?php print $data->getTotalResults();?> </td></tr> 
</table> 
</html> 
<?php 

?> 

代碼教程撕開Google Service Account with PHP

如果這不是您的帳戶,然後就可以使用正常的oauth2方法要求使用你使用刷新令牌認證一次,那麼你就能夠訪問數據。使用您之前問題的代碼。

+0

'在D:\ xampp \ htdocs \ ga_api \ google-api-php中發生'未知的異常'Google_Auth_Exception'消息'錯誤刷新OAuth2令牌,消息:'{「error」:「invalid_grant」}「 -client-master \ src \ Google \ Auth \ OAuth2.php:358'你能告訴我什麼是錯的嗎? – Sayed 2015-02-25 07:13:03

+0

檢查您的計算機上的Google無效授權時間。在使用它之前,驗證過期是錯誤的。 – DaImTo 2015-02-25 07:14:54

+0

如果您可以回答,我該如何與Google同步我的機器的時間? – Sayed 2015-02-25 07:33:38