我有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}
然而,当我尝试发送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网址复制并粘贴到我的浏览器上,我得到了
{"error":{"errors":[{"domain":"global","reason":"required","message":"Login Required","locationType":"header","location":"Authorization"}],"code":401,"message":"Login Required"}}
我也尝试使用Google OAuth 2.0游乐场,在第2步,我输入我的刷新令牌并点击“刷新访问令牌”,我遇到了unauthorized_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>";
}
?>