我正在開發一款應用程序,允許用戶使用Google API v3查看我自己的Google Analytics數據。 我研究的一切似乎都表明用戶需要登錄到他們的Google帳戶,並在開始查詢API之前授予我的應用訪問權限;不過,這不是我想要的,我只需要我的用戶查看我自己的Google Analytics數據。如何授權API訪問我的數據。 我有客戶端ID和客戶端密鑰,但是這通過谷歌的API V3實現的OAuth是要求授權令牌,只能通過獲取用戶登錄到自己的谷歌賬戶獲得(是嗎?) 有隻需登錄我自己的Google Analytics帳戶並向用戶顯示該信息的方法?Google Analytics API v3授權,允許訪問我的數據
回答
可以使用refresh token
脫機訪問。獲得refresh token
後,您可以將其保存到文件或數據庫,並使用該文件訪問未經授權重定向的數據。
另見:How can we access specific Google Analytics account data using API?
我相信你想要做的是建立一個服務帳戶什麼: https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtAuthorization
「可用於自動/離線/定期訪問谷歌Analytics(分析)數據您自己的帳戶。例如,建立自己的谷歌Analytics(分析)數據的實時信息中心,並與其他用戶共享。
有你需要FO幾步只需配置服務帳戶即可與Google Analytics配合使用:
- 在API控制檯中註冊一個項目。
- 在谷歌API控制檯的API訪問窗格下,創建一個設置爲服務帳戶的應用程序類型 客戶端ID。
- 登錄到Google Analytics並導航到管理部分。
- 選擇您希望應用程序訪問 的帳戶。
- 添加的電子郵件地址,從第2步在API 控制檯創建客戶端ID,如選擇谷歌分析 帳戶的用戶。
- 遵循服務帳戶說明訪問谷歌Analytics(分析)數據:https://developers.google.com/accounts/docs/OAuth2ServiceAccount「
這是一個完整的谷歌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>';
}
}
?>
該代碼適用於我...自Google_AssertionCredientials => Google_Auth_AssertionCredentials和Google_AnalyticsService => Google_Service_Analytics – Graben 2015-01-15 03:08:49
以來,類名稱已更改謝謝您進行編輯 – 2015-01-19 16:44:07
- 1. 谷歌Analytics API V3 - 授權API KEY(JAVA)
- 2. Google API資源管理器 - Google Analytics - v3 - 「401未授權」
- 3. 訪問控制允許標題不允許授權
- 4. 允許在沒有授權的情況下訪問單個API?
- 5. 訪問Google API時的授權無效
- 6. 使用Google Maps JavaScript API訪問控制允許來源v3
- 7. 允許Google服務帳戶YouTube通過API v3上傳訪問
- 8. java中的Google Analytics授權
- 9. Google Analytics API多賬戶授權
- 10. Google API訪問授權 - 需要指導
- 11. Mailchimp API v3授權
- 12. 授予Google服務帳戶訪問我所有的Google Analytics帳戶的權限
- 13. Google Drive API授權訪問,無需人工授權
- 14. YouTube API v3 Java授權
- 15. OAuth授權標頭是否允許我以授權用戶的身份訪問網站本身(不是API)?
- 16. 使用apikey訪問Google analytics v3
- 17. Google AnalyticsAPI - Google Analytics媒體資源授權
- 18. PHP Array和Google Analytics V3 API
- 19. 允許我的API通過AJAX訪問
- 20. 爲Google Analytics嵌入API服務器端授權獲取訪問令牌
- 21. 服務器託管只允許授權的IP進行數據庫訪問
- 22. Youtube v3 api 401未授權
- 23. virtualmin不允許我訪問數據庫
- 24. 使用JavaScript和API訪問Google Analytics數據的問題
- 25. 允許未經授權的用戶訪問RichFaces Skin CSS文件
- 26. OAuth訪問Google Drive授權問題
- 27. Google數據(日曆)API授權授權 - 前端
- 28. Tastypie - 允許未經授權的用戶使用只讀權限,同時允許授權的寫入權限
- 29. ASP.NET授權允許「*」和「?」
- 30. 授權谷歌地圖api v3密鑰從https頁面訪問
我有完全相同的疑問句重刑...你找到答案了嗎? – lpdahito 2012-04-07 19:13:56
你找到了答案嗎? – 2013-01-27 11:55:55