![Picture of Vincent Picture of Vincent](/picture/user/1119939.png)
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;,
}