i am trying to access the data of my Analytics using the following code
<?php
require_once 'lib/google-api-php-client/src/apiClient.php';
require_once 'lib/google-api-php-client/src/contrib/apiAnalyticsService.php';
session_start();
$client = new apiClient();
$client->setApplicationName("Google Analytics PHP Starter Application");
$service = new apiAnalyticsService($client);
if (isset($_GET['logout'])) {
unset($_SESSION['token']);
}
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if ($client->getAccessToken()) {
$accounts = $service->management_accounts->listManagementAccounts();
print "<h1>Accounts</h1><pre>" . print_r($accounts, true) . "</pre>";
try {
$data = $service->data_ga->get('ga:29214712', '2012-01-01', '2012-01-15',
'ga:visits');
$_SESSION['token'] = $client->getAccessToken();
}
catch (apiServiceException $e) {
echo $e->getCode();
print_r($data);
}
$_SESSION['token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Connect Me!</a>";
}
?>
I get the following error
Error calling GET
https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A29214712&start-date=2012-04-04&end-date=2012-04-15&metrics=ga%3Avisits&key=AIzaSy*****zEJjDQFKS1KrJ***-frmTc1WP8ck:
(403) Forbidden
I have set the Client ID and other stuff in the config file directly.