Google Analytics API Error 403

177 views
Skip to first unread message

Harsha M V

unread,
May 25, 2012, 10:15:04 AM5/25/12
to google-api...@googlegroups.com
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.


Chirag Shah

unread,
May 29, 2012, 4:28:37 PM5/29/12
to google-api...@googlegroups.com
Hi Harsha,

The 403 error you're getting means the authorized user doesn't have access to the ga:29214712 reporting profile that is defined in your query.

Take a look at the HelloAnalyticsAPI.php sample in the examples/analytics/demo directory, and make sure you can connect to the API:

Also, take a look at the Google Analytics developer guide. It will describe how you can obtain the correct profile IDs:
Reply all
Reply to author
Forward
0 new messages