PHP Classes

Google OAuth versus refresh token

Recommend this page to a friend!

      PHP OAuth Library  >  PHP OAuth Library package blog  >  How to Implement PHP ...  >  All threads  >  Google OAuth versus refresh token  >  (Un) Subscribe thread alerts  
Subject:Google OAuth versus refresh token
Summary:Google OAuth versus refresh token
Messages:3
Author:Vincent
Date:2014-10-12 13:12:08
 

  1. Google OAuth versus refresh token   Reply   Report abuse  
Picture of Vincent Vincent - 2014-10-12 13:12:08
I'm having troubles using OAuth 2.0 and Google. I cannot make it work properly. Getting a token from Google works properly but the token needs to get refreshed every hour. I don't understand what to do when a token needs to get refreshed. I prefer connecting to Google Analytics. Is there something wrong in my code below? Can you please help me out? Thank you in advance!

// My code
$client = new oauth_client_class;
$client->debug = true;
$client->debug_http = true;
$client->server = 'Google';
$client->redirect_uri = 'https://domain.com/redirect_uri.php';
$client->access_type = 'offline';
$client->offline = true;
$client->access_token = '***'; // After one hour, I need to set this manually (or by interface for users)
$client->refresh_token = '***'; // After one hour, I need to set this manually (or by interface for users)
$client->client_id = '***';
$application_line = __LINE__;
$client->client_secret = '***';
$client->scope = 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/analytics.readonly';

if(strlen($client->client_id) == 0 || strlen($client->client_secret) == 0)
die('Google API Error');

if(($success = $client->Initialize()))
{
if(($success = $client->Process()))
{
if(strlen($client->access_token))
{
// Get credentials
$success = $client->CallAPI('https://www.googleapis.com/oauth2/v1/userinfo', 'GET', array(), array('FailOnAccessError' => true), $user);
}
}
$success = $client->Finalize($success);
}
if($client->exit) exit;

if($success)
{
// do something
echo print_r($user, true);
echo print_r($client, true;,
}

  2. Re: Google OAuth versus refresh token   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2014-10-12 23:35:48 - In reply to message 1 from Vincent
The class automatically refreshes the token when it expires but you need to store the new token from then on.

By default the class stores tokens in sessions but sessions do not exist when the user is not present. So you need to store it in a persistent container.

That is why there is a sub-class meant for storing tokens in database. It also stores refreshed tokens when old ones expire. Read this article to learn how it works:

phpclasses.org/blog/package/7700/po ...

  3. Re: Google OAuth versus refresh token   Reply   Report abuse  
Picture of Vincent Vincent - 2014-10-16 13:51:06 - In reply to message 2 from Manuel Lemos
Hello Manuel,

Thank you for your answer. I'll look into your article.

With kind regards,

Vincent