我正在尝试使用最新的PHP客户端API(https://github.com/google/google-api-php-client)检索 Google Analytics管理配置文件。
我有以下代码段:
// we've got the token, so set it
$google_client->setAccessToken($_SESSION['access_code']);
if ($google_client->getAccessToken()) {
$profiles = $google_analytics_service->management_profiles->listManagementProfiles("~all", "~all");
print "<h1>Profiles</h1><pre>" . print_r($profiles, true) . "</pre>";
}
/* $url = 'https://www.googleapis.com/analytics/v3/management/accounts/~all/webproperties/~all/profiles'; */
/* // json decode as array */
/* $analytics_auth = json_decode($_SESSION['access_code'], true); */
/* $ch = curl_init($url . '?access_token=' . $analytics_auth['access_token']); */
/* curl_exec($ch); */
/* curl_close($ch); */
上面提到的错误信息是:
调用GET https://www.googleapis.com/analytics/v3/management/accounts/~all/webproperties/~all/profiles?key=AIza[SNIP]时出错:(403)Access Not Configured
注意:但是我决定使用cURL运行相同的操作,并返回带有配置文件(注释代码)的JSON数组。这是一个错误,还是我?我注意到的是我的access_token以&#34; ya29&#34;开头。
答案 0 :(得分:6)
我认为你错过了一步:
<?php
require_once 'Google/Client.php';
require_once 'Google/Service/Analytics.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$client->setDeveloperKey("{developerkey}");
$client->setClientId('{clientID}.apps.googleusercontent.com');
$client->setClientSecret('{Client secret}');
$client->setRedirectUri('http://www.daimto.com/Tutorials/PHP/Oauth2.php');
$client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));
//For loging out.
if ($_GET['logout'] == "1") {
unset($_SESSION['token']);
}
// Step 2: The user accepted your access now you need to exchange it.
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
// Step 1: The user has not authenticated we give them a link to login
if (!$client->getAccessToken() && !isset($_SESSION['token'])) {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Connect Me!</a>";
}
// Step 3: We have access we can now create our service
if (isset($_SESSION['token'])) {
print "<a class='logout' href='".$_SERVER['PHP_SELF']."?logout=1'>LogOut</a><br>";
$client->setAccessToken($_SESSION['token']);
$service = new Google_Service_Analytics($client);
// request user accounts
$accounts = $service->management_accountSummaries->listManagementAccountSummaries();
foreach ($accounts->getItems() as $item) {
echo "Account: ",$item['name'], " " , $item['id'], "<br /> \n";
foreach($item->getWebProperties() as $wp) {
echo ' WebProperty: ' ,$wp['name'], " " , $wp['id'], "<br /> \n";
$views = $wp->getProfiles();
if (!is_null($views)) {
foreach($wp->getProfiles() as $view) {
// echo ' View: ' ,$view['name'], " " , $view['id'], "<br /> \n";
}
}
}
} // closes account summaries
}
print "<br><br><br>"; // fix syntax
print "Access from google: " . $_SESSION['token'];
?>
由于session_start和标题的问题,它有点乱序。我添加了一些评论来帮助您了解它的作用。它是一个简单的脚本,但你可以在这里测试Dummy Example for Hal9k
答案 1 :(得分:0)
403问题是因为没有为服务器应用程序密钥设置正确的IP地址。这可以在Google Developers&#39;中设置。安慰。然而,奇怪的是,使用cURL绕过了这个。
公共API访问 使用此密钥不需要任何用户操作或同意,不授予对任何帐户信息的访问权限,也不用于授权。
服务器应用程序的密钥API密钥:AI [snip] IP:91。[snip] 109. [snip] 确保在此设置正确的IP地址