Re: How to retrieve access token from refresh token?

2,784 views
Skip to first unread message

Aurelien D

unread,
Jun 28, 2012, 5:01:51 PM6/28/12
to google-api...@googlegroups.com
Here is the PHP  code i use once i have the refreshToken and the user has authorized his data to be used.

It will normally give you a new access_token with a new expiring time. It will echo it on the screen and also connect to the calendar api (you have to activate it it the google api console) to return the possible colors of calendar and the list of the calendars.

Hope to answer well to your question.

Aurel

session_start();
define("ABSPATHLIBRARY","../../../library/"); // your path to library files

require_once ABSPATHLIBRARY."google-api-php-client/src/apiClient.php";
require_once ABSPATHLIBRARY."google-api-php-client/src/contrib/apiCalendarService.php";
require_once ABSPATHLIBRARY."google-api-php-client/src/contrib/apiOauth2Service.php";

$refreshToken='1/kLJV0khYr.....your Refresh token here';

$client = new apiClient();
$client->setUseObjects(true);

$client->setRedirectUri('your redirect Uri');
$client->setAccessType('offline');
$client->refreshToken($refreshToken);

$calService = new apiCalendarService($client);

if (isset($_SESSION['token'])) 
{
$decoded=json_decode($client->getAccessToken());
//print_r($decoded);
//return;
$client->setAccessToken($_SESSION['token']);
}

if ($client->getAccessToken()) {
echo "<h1>Access Token</h1>";
$decoded=$client->getAccessToken();
print_r($decoded);
echo "<br />";
$array=get_object_vars(json_decode($decoded));
$accessToken=$array['access_token'];
echo "access token = ".$accessToken."<br />";
$colors = $calService->colors->get();

// Print available calendarListEntry colors.
foreach ($colors->getCalendar() as $key => $color) {
  print "CALENDAR colorId : {$key}\n";
  print "<div style='background-color:".$color->getBackground().";'><font style='color:".$color->getForeground().";'>FONT</font></div>\n<br>";
}
// Print available event colors.
foreach ($colors->getEvent() as $key => $color) {
  print "EVENT colorId : {$key}\n";
  print "<div style='background-color:".$color->getBackground().";'><font style='color:".$color->getForeground().";'>FONT</font></div>\n<br>";
}
   $calList = get_object_vars($calService->calendarList->listCalendarList());
   echo "<h1>List of calendars</h1>";
$calList = $calList['items'];
foreach ($calList as $calCarac)
{
$calCarac=get_object_vars($calCarac);
$idCal = $calCarac['id'];
echo $idCal." ".$calCarac['summary']."<br>";
if ($calCarac['summary'] == 'OURSIN')
$calendarId = $calCarac['id'];
}

$_SESSION['token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
   print "<a class='login' href='$authUrl'>Connect Me!</a>";


2012/6/26 Sangu <sanga...@mink7.com>
I am using google api php client.. I wanna get the analytics data by offline, so I am storing the refresh token into database.. 
From that refresh_token, I am not getting the analytics data.. So i need an access_token.. But I am in confusion that am i save the 
access_token, id_token, refresh_token and all.. But the problem is, the access_token expires after some time.. So I need an access token..
Please help me to get the access_token..

Message has been deleted
Message has been deleted

Aurelien D

unread,
Jul 13, 2012, 9:27:43 AM7/13/12
to google-api...@googlegroups.com
you are welcome :)

Aurel

2012/7/13 Veronica Cisneros <ad...@iberoamericamedia.com>
Of course you are... I am sorry... spoke too soon...

Disregard my message. Thank you



On Friday, July 13, 2012 12:19:40 AM UTC-7, Veronica Cisneros wrote:
Why not use 

$refreshToken = retrieve from database...

$new_token = client -> refreshToken($refreshToken);

from apiClient.php

  /**
   * Fetches a fresh OAuth 2.0 access token with the given refresh token.
   * @param string $refreshToken
   * @return void
   */
  public function refreshToken($refreshToken) {
    self::$auth->refreshToken($refreshToken);
  }

Jean-Louis curty

unread,
Jan 29, 2013, 5:31:16 PM1/29/13
to google-api...@googlegroups.com
hi, I managed to access my own calendar using oauth v2 but I need to login everytime.
I would like to be able to access customer calendars to add/remove event like in this service :www.appointlet.com 
is your code below the right approach to reach this goal ?
thanks 
jl

Aurelien D

unread,
Jan 30, 2013, 12:54:45 AM1/30/13
to google-api...@googlegroups.com
the code above needs to be updated to 0.6 version of php google api library.

use this page https://code.google.com/p/google-api-php-client/ to help yourself to change the names of the included files and classes which are used in the code (changes are just often adding a Google_ prefix instead of Api or what).

Aurel

2013/1/29 Jean-Louis curty <jlc...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "google-api-php-client" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-api-php-c...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Eric Nguyen

unread,
Feb 26, 2013, 10:47:12 PM2/26/13
to google-api...@googlegroups.com
Thanks a lot for sharing, Aurelien!

Would you be able to help with my question here?
I believe my question is close enough to what you share here and I'm very near to achieving my goal as well.

Thank you in advance!
Reply all
Reply to author
Forward
0 new messages