【问题标题】:Google Analytics API v3 authorization to allow access to my dataGoogle Analytics API v3 授权允许访问我的数据
【发布时间】:2012-04-13 11:54:27
【问题描述】:

我正在开发一个应用程序,允许用户使用 Google API v3 查看我自己的 Google Analytics(分析)数据。 我研究的所有内容似乎都表明用户需要登录他们的 Google 帐户并授予我的应用访问权限,然后我才能开始查询 API;然而,这不是我想要的,我只需要我的用户看到我自己的分析数据。如何授权 API 访问我的数据。 我有客户端 ID 和客户端密码,但由 Google API v3 实现的 OAuth 要求提供授权令牌,这只能通过让用户登录到他们的谷歌帐户来获得(对吗?) 有没有办法只登录我自己的 Google Analytics 帐户并向用户显示该信息?

【问题讨论】:

  • 我也有同样的问题...你找到答案了吗?
  • 你找到答案了吗?

标签: api oauth google-analytics google-analytics-api


【解决方案1】:

这是一个完整的 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>';        
  }
}
?>

更多文档的代码在https://gist.github.com/fulldecent/6728257

【讨论】:

  • 该代码对我有用...自从 Google_AssertionCredentials => Google_Auth_AssertionCredentials 和 Google_AnalyticsService => Google_Service_Analytics 之后,类名称已更改
【解决方案2】:

我相信您要做的是设置一个服务帐户https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtAuthorization

“对于自动/离线/计划访问您自己的帐户的 Google Analytics(分析)数据很有用。例如,构建您自己的 Google Analytics(分析)数据的实时仪表板并与其他用户共享。

您需要遵循几个步骤来配置服务帐户以使用 Google Analytics:

  1. 在 API 控制台中注册项目。
  2. 在 Google API 控制台的 API 访问窗格下,创建一个 应用程序类型设置为服务帐户的客户端 ID。
  3. 登录 Google Analytics 并导航到管理部分。
  4. 选择您希望应用程序有权访问的帐户 到。
  5. 从 API 中创建的客户端 ID 添加电子邮件地址 来自第 2 步的控制台,作为所选 Google Analytics 的用户 帐户。
  6. 按照服务帐户的说明访问 Google Analytics 数据:https://developers.google.com/accounts/docs/OAuth2ServiceAccount"

【讨论】:

    【解决方案3】:

    您可以使用refresh token 进行离线访问。获得refresh token 后,您可以将其保存到文件或数据库中,并使用它来访问数据而无需授权重定向。

    请参阅文档中的 Using a Refresh Token

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

    【讨论】:

    • 我认为要获得更强大的解决方案,您应该使用服务帐户答案
    • @mattl 我不认为这是拒绝投票的理由。答案没有错,它只是 Google 提供的选项之一。
    • 你说得对,对不起。我现在无法更改它,因为它已经超过 20 小时了。其他人可以再次投票吗?
    猜你喜欢
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-23
    • 2013-07-21
    • 2017-01-08
    相关资源
    最近更新 更多