4

auth.phpGoogle Analyticsへのアクセスを承認し、更新トークンを取得してテキストファイルに保存するためのリンクがありますtoken.txt。更新トークンを正常に取得し、それをtoken.txt正常に保存しました。
次にtest.php、更新トークンを取得して、token.txtオフラインでGoogle Analyticsにアクセスしようとします(Webサイトの訪問者にページビューを表示できるようにするため)。後$client->refreshToken($refreshToken)$client->getAccessToken()アクセストークンを印刷できたので成功したようです。{"access_token":"ya29.AHES6ZRZKhDxMMpTrzif-xXaTQMhYNTlFLu1hQgdb45b0sZ8w8eSJQ","expires_in":3600,"created":1361934280}

しかし、getリクエストを送信しようとすると、

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プレイグラウンドも使用してみましたが、手順2で、更新トークンを入力して[アクセストークンの更新]をクリックすると、authorized_clientエラーが発生しました。

ここで何が欠けていますか?

test.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>";
}

?>

auth.php

<?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