Question

I'm trying to let my users add me (using my email address) as a read-only user to their Google Analytics account via the Google Analytics API. In essence, they'd access my PHP page where it says: "Connect to Google Analytics" and they'd click on it, and give me read-only access - NOT JUST for this one time, but forever (unless they remove my email).

So far, I tried this:

$client = new Google_Client();
$client->setApplicationName('My App');
$client->setClientId('my client ID');
$client->setClientSecret('my client secret');
$client->setRedirectUri('my redirect URI');
$client->setDeveloperKey('my developer key');
$client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));

My script receives a token just fine, but it can only access the data this one time. How can I make sure I can access the users Google Analytics data whenever I want?

Was it helpful?

Solution

The code above gives your application access to there Google Analytics account. Not you personally via your email address. There is a difference you wont be able to log into the Google Analytics website and see there site as part of your account list. But your application (if you save the refreshToken) will be able to access there data.

The reason you can only access the data the one time is probably because you aren't saving the Refreshtoken and sending it the next time to get a new access Token

A refresh token is obtained in offline scenarios during the first authorization code exchange. In these cases, your application may obtain a new access token by sending a refresh token to the Google OAuth 2.0 Authorization server.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top