Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Analytics Core Reporting API Version 3.0 without client login

I want to access our Google Analytics account reporting using the newer v3.0, but it seems from everything that I read that in order to get a valid access token the user must log in.

We want direct access to our own account reporting, and not access a client's depending on their account. How do we accomplish this in PHP without having to send the browser to a Google login page? Is there no straight API authentication for v3.0?

EDIT

This seems to be the only method of accessing the API without end-user interaction, which they call "Server to Server":

https://developers.google.com/accounts/docs/OAuth2ServiceAccount

EDIT 2

Looks like it can't be done? ;(

Warning: Very few Google APIs currently support Service Accounts. Service accounts are currently supported by the following Google developer services:

  • Google Cloud Storage
  • Google Prediction API
  • Google URL Shortener
  • Google OAuth 2.0 Authorization Server

EDIT 3

There seems to be a solution after all, as I log in once and then use "Refresh Tokens" to keep gaining access without an additional user login.

like image 923
Sarke Avatar asked Aug 15 '12 09:08

Sarke


People also ask

Is Google Analytics reporting API free?

The Google Analytics API is free to use. You don't have to pay for using it. If you run out of quota you can request an increase on the 50000 requests a day.

How do I enable Google Analytics API reporting?

To get started using Analytics API, you need to first use the setup tool, which guides you through creating a project in the Google API Console, enabling the API, and creating credentials. To set up a new service account, do the following: Click Create credentials > Service account key.

Which are features of the Core reporting API?

With the Core Reporting API you can: Build custom dashboards to display Google Analytics data. Save time by automating complex reporting tasks. Integrate your Google Analytics data with other business applications.


1 Answers

I did end up using the refresh tokens, they work fine. I got a oauth token by using the google api console, and then saved it.

Then I just do this before each request:

require_once 'google-api-php-client/src/apiClient.php';
require_once 'google-api-php-client/src/contrib/apiAnalyticsService.php';;

$client = new apiClient();
$client->setApplicationName('My Analytics');
$client->setClientId($this->client_id);
$client->setClientSecret($this->client_secret);
$client->setDeveloperKey($this->api_key);

$client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));

$client->refreshToken($this->refresh_token);

$this->service = new apiAnalyticsService($client);
like image 108
Sarke Avatar answered Sep 30 '22 06:09

Sarke