PHP中的分析报告API:(403)权限不足

时间:2014-09-17 03:59:18

标签: php google-api-php-client

这是我的代码。

<?php

session_start();
include_once "templates/base.php";

set_include_path("../src/" . PATH_SEPARATOR . get_include_path());
require_once 'Google/Client.php';
require_once 'Google/Service/Books.php';
require_once 'Google/Service/Analytics.php';

$client_id = '...'; //Client ID
$service_account_name = '...'; //Email Address 
$key_file_location = 'key.p12'; //key.p12

echo pageHeader("Service Account Access");
if ($client_id == '<YOUR_CLIENT_ID>'
    || !strlen($service_account_name)
    || !strlen($key_file_location)) {
  echo missingServiceAccountDetailsWarning();
}

$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
//$service = new Google_Service_Books($client);
$service = new Google_Service_Analytics($client);

if (isset($_SESSION['service_token'])) {
  $client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
    $service_account_name,
    array('https://www.googleapis.com/auth/analytics.readonly'),
    $key
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
  $client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();

$analytics_id   = 'ga:88243354';
$lastWeek       = date('Y-m-d', strtotime('-1 week'));
$today          = date('Y-m-d');
try {
    $results = $service->data_ga->get($analytics_id,
                        $lastWeek,
                        $today,'ga:sessions');
    echo '<b>Number of visits this week:</b> ';
    echo $result;
    echo $results['totalsForAllResults']['ga:ssessions'];
} catch(Exception $e) {
    echo 'There was an error : - ' . $e->getMessage();
}

echo pageFooter(__FILE__);

我收到错误消息

  

错误(403)权限不足。

当然,我需要在我的开发控制台上打开Analytics API。 并将客户端电子邮件添加到Google个人资料用户。

我需要帮助; - ;

0 个答案:

没有答案