4

我有auth.php一个链接供我授权访问 Google Analytics 并获取刷新令牌,将其存储到文本文件token.txt中。我得到了刷新令牌并保存了它token.txt
然后我会test.php从中获取刷新令牌token.txt并尝试离线访问 Google Analytics(以便我可以向我网站的访问者显示综合浏览量)。之后$client->refreshToken($refreshToken)$client->getAccessToken()似乎成功了,因为我可以打印出访问令牌,例如{"access_token":"ya29.AHES6ZRZKhDxMMpTrzif-xXaTQMhYNTlFLu1hQgdb45b0sZ8w8eSJQ","expires_in":3600,"created":1361934280}

但是,当我尝试发送获取请求时,我得到了

Error calling GET https://www.googleapis.com/analytics/v3/data/ga?ids=ga%xxx&start-date=2012-01-06&end-date=2013-02-27&metrics=ga%3Apageviews&filters=ga%3ApagePath%3D%3D%2Fsome-article%2F&max-results=1&key=AIzaSyC6G0bDA-zsBGKRAHEj4JnSgF95dt_nE6g: (403) User does not have sufficient permissions for this profile.

将GET url复制并粘贴到我的浏览器上,我得到了

{"error":{"errors":[{"domain":"global","reason":"required","message":"Login Required","locationType":"header","location":"Authorization"}],"code":401,"message":"Login Required"}}

我也尝试使用 Google OAuth 2.0 Playground,在第 2 步,我输入我的刷新令牌并单击“刷新访问令牌”,我得到了未授权客户端错误。

我在这里想念什么?

测试.php

<?php
require_once dirname(__FILE__).'/src/Google_Client.php';
require_once dirname(__FILE__).'/src/contrib/Google_AnalyticsService.php';

$client = new Google_Client();
$client->setApplicationName("Google Analytics PHP Starter Application");

$client->setClientId('xxxx.apps.googleusercontent.com');
$client->setClientSecret('yyyy');
$client->setRedirectUri('http://app.tickledmedia.com/google/auth.php');
$client->setDeveloperKey('zzzz');

$myFile = "token.txt";
$fh = fopen($myFile, 'r');
$refreshToken = fread($fh, filesize($myFile));
fclose($fh);

$client->setAccessType('offline');
$client->refreshToken($refreshToken);

$service = new Google_AnalyticsService($client);

if ($client->getAccessToken()) {
    print_r($client->getAccessToken());

    $ids = "ga:xxxx";
    $start_date = "2012-01-06";
    $end_date = date("Y-m-d");
    $metrics = "ga:pageviews";
    //$dimensions = "ga:browser";
    $filters = "ga:pagePath==/some-article/";
    $maxresults = 1;
    $optParams = array('filters'=>$filters, 'max-results' => $maxresults);
    $data = $service->data_ga->get($ids,$start_date,$end_date, $metrics, $optParams);

    print_r($data);

} else {
  $authUrl = $client->createAuthUrl();
  print "<a class='login' href='$authUrl'>Connect Me!</a>";
}

?>

授权文件

<?php
require_once dirname(__FILE__).'/src/Google_Client.php';
require_once dirname(__FILE__).'/src/contrib/Google_AnalyticsService.php';
session_start();

$client = new Google_Client();
$client->setApplicationName("Google Analytics PHP Starter Application");

$client->setClientId('xxxx.apps.googleusercontent.com');
$client->setClientSecret('yyyy');
$client->setRedirectUri('http://app.mydomain.com/google/auth.php');
$client->setDeveloperKey('zzzz');
$service = new Google_AnalyticsService($client);

if (isset($_GET['logout'])) {
  unset($_SESSION['token']);
}

echo "the refresh token stored is ";
if ($_SESSION["refresh_token"]) echo $_SESSION["refresh_token"];
else {
    $myFile = "token.txt";
    $fh = fopen($myFile, 'r');
    $refreshToken = fread($fh, filesize($myFile));
    fclose($fh);
    echo $refreshToken;
}

if (isset($_GET['code'])) {
  $client->authenticate();
  $token = $client->getAccessToken();

  //var_dump($token);
  $my_token = json_decode($token, true);
  $_SESSION["my_token"] = $my_token;

  $refreshToken = $my_token["refresh_token"];
  $_SESSION["refresh_token"] = $my_token["refresh_token"];
  $myFile = "token.txt";
  $fh = fopen($myFile, 'w') or die("can't open file");
  fwrite($fh, $refreshToken);
  fclose($fh);

  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}

if ($client->getAccessToken()) {
  $props = $service->management_webproperties->listManagementWebproperties("~all");
  print "<h1>Web Properties</h1><pre>" . print_r($props, true) . "</pre>";

  $_SESSION['token'] = $client->getAccessToken();
  print_r($_SESSION["my_token"]);
} else {
  $authUrl = $client->createAuthUrl();
  print "<a class='login' href='$authUrl'>Connect Me!</a>";
}

?>
4

0 回答 0