How to use refresh token with google api client

5,501 views
Skip to first unread message

Julian Tiemann

unread,
Apr 19, 2013, 4:11:06 AM4/19/13
to google-analytics...@googlegroups.com
Hi,

I really dont got how to give the application permanent access to the google analytics api by using a refresh token.
I'm using the simple google-api-php-client and i dont know how to get offline access.
I set the AccessType to offline, but still every time i close my browser i have to login again.

$client = new Google_Client();
$client->setClientId(CLIENT_ID);
$client->setClientSecret(CLIENT_SECRET);
$client->setRedirectUri(REDIRECT_URL);
$client->setApplicationName(APP_NAME);
$client->setAccessType('offline');
$client->setScopes(array(ANALYTICS_SCOPE));

I use the simple /example/analytics/demo/index.php without any changes. Im really new to this OAuth stuff the google documentation is not a big help at all, as it's not referring to the google-api-php-client.
The thing is, that the refresh token would need to be stored somewhere right? But I dont see a function like this in the demo.
So how would I do that?

There are only some urls and no word about how to use them - Do I have to post them with curl?
Seems I have to find a different way without google-api-php-client.

Thanks a lot

Phil Gyford

unread,
Apr 25, 2013, 9:42:39 AM4/25/13
to google-analytics...@googlegroups.com
I haven't used the PHP library, but looking at the source for the client[1] it does look like the getAccessToken() and setAccessToken() methods deal with refresh_tokens. So in the "Getting up and running in 60 seconds" example on the library's front page, the content of $_SESSION['token'] isn't just a token as a string, but a JSON-encoded structure like this:

{"access_token":"TOKEN", "refresh_token":"TOKEN", "token_type":"Bearer",
"expires_in":3600,"id_token":"TOKEN", "created":1320790426}

When you call Google_Client::setAccessToken(), this calls Google_OAuth2::setAccessToken() which keeps that structure... and when Google_OAuth2::sign() is called (I'm not sure when that happens) it does deal with the refresh_token.[2]

All of which is by way of saying it looks like the code knows to do the right thing with refresh_tokens etc, so I'm guessing it's something that's not working right in your code... are you doing as the example says and storing the access token anywhere?

  // We're not done yet. Remember to update the cached access token.
 
// Remember to replace $_SESSION with a real database or memcached.
  $_SESSION
['token'] = $client->getAccessToken();


Nick

unread,
Apr 25, 2013, 1:24:56 PM4/25/13
to google-analytics...@googlegroups.com
Hi.


I've implemented 2 types of storage for this:
- session based
- cookie based

both are implemented in the storage.php class: https://code.google.com/p/google-api-php-client/source/browse/trunk/examples/analytics/demo/storage.php

To use one of these, I instantiate an instance in index.php line 85, and pass by reference in the authHelper class.
The AuthHelper handles all the logic of setting, retrieving, etc....

You can change the type of auth by instantiating a different class.

Based on the status of Auth, I modify the page, authorize a client, etc....all in index.php


-Nick
Reply all
Reply to author
Forward
0 new messages