我正在开发一款应用,允许用户使用Google API v3查看自己的Google Analytics数据。 我研究过的所有内容似乎都表明,在我开始查询API之前,用户需要登录自己的Google帐户并授予我的应用访问权限;但是,这不是我想要的,我只需要我的用户查看我自己的Google Analytics数据。如何授权API访问我的数据。 我有客户端ID和客户端密码,但是Google API v3实施的OAuth要求授权令牌,只有让用户登录他们的谷歌帐户才能获得授权令牌(是吗?) 有没有办法登录我自己的Google Analytics帐户并向用户显示该信息?
答案 0 :(得分:11)
我相信您要做的是设置服务帐户: https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtAuthorization
“对您自己的帐户自动/离线/预定访问Google Analytics数据非常有用。例如,构建自己的Google Analytics数据的实时信息中心并与其他用户共享。
要配置服务帐户以使用Google Analytics,您需要执行以下几个步骤:
- 在API控制台中注册项目。
- 在Google API控制台的“API访问”窗格下,创建一个 “应用程序类型”设置为“服务帐户”的客户端ID。
- 登录Google Analytics并导航至“管理”部分。
- 选择您希望应用程序有权访问的帐户 到。
- 从API中创建的客户端ID添加电子邮件地址 步骤2中的控制台,作为所选Google Analytics的用户 帐户。
- 按照服务帐户的说明访问Google Analytics数据:https://developers.google.com/accounts/docs/OAuth2ServiceAccount“
醇>
答案 1 :(得分:2)
您可以使用refresh token
进行离线访问。获得refresh token
后,您可以将其保存到文件或数据库,并使用它来访问数据而无需授权重定向。
请参阅文档中的Using a Refresh Token。
另见:How can we access specific Google Analytics account data using API?
答案 2 :(得分:0)
以下是包含设置说明的服务帐户的完整Google Analytics报告示例实施。刚刚在阅读完问题后写了这篇文章,我遇到了同样的问题。
<?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>';
}
}
?>