2012-03-29 82 views
12

我正在開發一款應用程序,允許用戶使用Google API v3查看我自己的Google Analytics數據。 我研究的一切似乎都表明用戶需要登錄到他們的Google帳戶,並在開始查詢API之前授予我的應用訪問權限;不過,這不是我想要的,我只需要我的用戶查看我自己的Google Analytics數據。如何授權API訪問我的數據。 我有客戶端ID和客戶端密鑰,但是這通過谷歌的API V3實現的OAuth是要求授權令牌,只能通過獲取用戶登錄到自己的谷歌賬戶獲得(是嗎?) 有隻需登錄我自己的Google Analytics帳戶並向用戶顯示該信息的方法?Google Analytics API v3授權,允許訪問我的數據

+3

我有完全相同的疑問句重刑...你找到答案了嗎? – lpdahito 2012-04-07 19:13:56

+0

你找到了答案嗎? – 2013-01-27 11:55:55

回答

2

可以使用refresh token脫機訪問。獲得refresh token後,您可以將其保存到文件或數據庫,並使用該文件訪問未經授權重定向的數據。

看到文檔Using a Refresh Token

另見:How can we access specific Google Analytics account data using API?

+0

我認爲對於更強大的解決方案,您應該使用服務帳戶答案 – mattl 2013-07-11 11:27:06

+0

@mattl我不認爲這是投票的理由。答案並不錯,這只是Google提供的選項之一。 – 2013-07-11 14:04:59

+0

你說得對,我很抱歉。我已經超過20個小時了,現在無法更改。其他人可以再次高興嗎? – mattl 2013-07-12 07:50:56

10

我相信你想要做的是建立一個服務帳戶什麼: https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtAuthorization

「可用於自動/離線/定期訪問谷歌Analytics(分析)數據您自己的帳戶。例如,建立自己的谷歌Analytics(分析)數據的實時信息中心,並與其他用戶共享。

有你需要FO幾步只需配置服務帳戶即可與Google Analytics配合使用:

  1. 在API控制檯中註冊一個項目。
  2. 在谷歌API控制檯的API訪問窗格下,創建一個設置爲服務帳戶的應用程序類型 客戶端ID。
  3. 登錄到Google Analytics並導航到管理部分。
  4. 選擇您希望應用程序訪問 的帳戶。
  5. 添加的電子郵件地址,從第2步在API 控制檯創建客戶端ID,如選擇谷歌分析 帳戶的用戶。
  6. 遵循服務帳戶說明訪問谷歌Analytics(分析)數據:https://developers.google.com/accounts/docs/OAuth2ServiceAccount
0

這是一個完整的谷歌Analytics(分析)報告與包括安裝Notes服務帳戶例如實現只寫讀你的問題後,我。有同樣的問題。

<?php 
// Service account code from http://stackoverflow.com/questions/18258593/using-a-service-account-getaccesstoken-is-returning-null 
// Analytics code from https://code.google.com/p/google-api-php-client/source/browse/trunk/examples/analytics/simple.php?r=474 

require_once 'google-api-php-client/src/Google_Client.php'; 
require_once 'google-api-php-client/src/contrib/Google_AnalyticsService.php'; 

// Set your client id, service account name (AKA "EMAIL ADDRESS"), and the path to your private key. 
// For more information about obtaining these keys, visit: 
// https://developers.google.com/console/help/#service_accounts 
const CLIENT_ID = 'CLIENT ID'; 
const SERVICE_ACCOUNT_NAME = 'SERVICE ACCOUNT NAME (IS "EMAIL ADDRESS")'; 
const KEY_FILE = 'KEY FILE'; 
const SCOPE = 'https://www.googleapis.com/auth/analytics.readonly'; 

// OPEN GOOGLE ANALYTICS AND GRANT ACCESS TO YOUR PROFILE, THEN PASTE IN YOUR SERVICE_ACCOUNT_NAME 

$key = file_get_contents(KEY_FILE); 
$auth = new Google_Auth_AssertionCredentials(
    SERVICE_ACCOUNT_NAME, 
    array(SCOPE), 
    $key 
); 

$client = new Google_Client(); 
$client->setScopes(array(SCOPE)); 
$client->setAssertionCredentials($auth); 
$client->getAuth()->refreshTokenWithAssertion(); 
$accessToken = $client->getAccessToken(); 
$client->setClientId(CLIENT_ID); 
$service = new Google_Service_Analytics($client); 

?> 
<!DOCTYPE html> 
<html> 
    <head> 
    <title>Google Experiments Dashboard</title> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" media="screen"> 
    </head> 
    <body class="container"> 
    <h1>Your experiments</h1> 
    <table class="table"><tr><th><th>Experiment<th>Page<th>Started<th>Status 
<?php 
$progressClasses = array('progress-bar progress-bar-success','progress-bar progress-bar-info','progress-bar progress-bar-warning', 'progress-bar progress-bar-danger'); 
$profiles = $service->management_profiles->listManagementProfiles("~all", "~all"); 

foreach ($profiles['items'] as $profile) { 
    $experiments = $service->management_experiments->listManagementExperiments($profile['accountId'], $profile['webPropertyId'], $profile['id']); 

    foreach ($experiments['items'] as $experiment) { 
    echo "<tr>"; 
    if ($experiment['status'] == 'RUNNING') 
     echo '<td><a class="btn btn-xs btn-success"><i class="glyphicon glyphicon-ok"></i></a>'; 
    else 
     echo '<td><a class="btn btn-xs btn-danger"><i class="glyphicon glyphicon-remove"></i></a>'; 
    $expHref = "https://www.google.com/analytics/web/?pli=1#siteopt-experiment/siteopt-detail/a{$profile['accountId']}w{$experiment['internalWebPropertyId']}p{$experiment['profileId']}/%3F_r.drilldown%3Danalytics.gwoExperimentId%3A{$experiment['id']}/"; 
    echo "<td><a href='$expHref' target='_blank'>{$experiment['name']}</a>"; 
    echo "<td>{$experiment['variations'][0]['url']}"; 
    echo "<td>".date('Y-m-d',strtotime($experiment['startTime'])); 
    echo "<td>"; 

    echo '<div class="progress" style="width:400px">'; 
    foreach ($experiment['variations'] as $i => $variation) { 
     echo '<a href="'.$variation['url'].'" target="_blank"><div class="'.$progressClasses[$i].'" role="progressbar" style="width: '.(100*$variation['weight']).'%">'.$variation['name'].'</div></a>'; 
    } 
    echo '</div>';   
    } 
} 
?> 

代碼更多的文檔以https://gist.github.com/fulldecent/6728257

+0

該代碼適用於我...自Google_AssertionCredientials => Google_Auth_AssertionCredentials和Google_AnalyticsService => Google_Service_Analytics – Graben 2015-01-15 03:08:49

+0

以來,類名稱已更改謝謝您進行編輯 – 2015-01-19 16:44:07

相關問題