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..