0

Hello All,
I am using google calendaer API PHP libraries.
but refreshToken is not working for me.
Please tell me where am wrong in this code:

<?php
require_once 'src/Google_Client.php';
require_once 'src/contrib/Google_CalendarService.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("Google Calendar PHP Starter Application"); 
$client->setAccessType('offline'); // added this to handle offline section

$cal = new Google_CalendarService($client);
if (isset($_GET['logout'])) 
{
  unset($_SESSION['token']);
}

if (isset($_GET['code'])) 
{
  $client->authenticate($_GET['code']);
  $_SESSION['token'] = $client->getAccessToken();
  //echo "in the GET['code']: ". $_SESSION['token'] . "is session token<br>";
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

//check whether tocken is expired or not
if($client->isAccessTokenExpired()) 
{   echo " Access Token Expired<br><br>"; }

if (isset($_SESSION['token'])) 
{
  //echo $_SESSION['token'] . ": <b>is session token in the </b>isset(_SESSION[token]) function";
    $client->setAccessToken($_SESSION['token']);
 //json decode the session token. Save it in a variable as object and 
    $sessionToken = json_decode($_SESSION['token']);

   echo "before refresh token: <br> " .$_SESSION['token'] . "<br><br>";
   $client->refreshToken($_SESSION["token"]);//update token
   //**********************before refresh it gives me value but after use of refreshToken it do not give********** 
   echo "after refresh token: <br> " .$_SESSION['token'] . "<br><br>";

   if (isset($sessionToken->refresh_token)) 
    { 
        //refresh token is only set after a proper authorisation
        $number_of_days = 30 ;
        $date_of_expiry = time() + 60 * 60 * 24 * $number_of_days ;
        setcookie('token', $sessionToken->refresh_token, $date_of_expiry);
    }
}

else if (isset($_COOKIE["token"])) 
{
    echo "<br>in else loop <br>";  
    //if we don't have a session we will grab it from the cookie
    // this is just saved to check whether session and cookie token valueas are same or not
    $token=$_COOKIE["token"]; 
    //echo "<b>1\/xGRQ4M9Sb3O4goVpFEVUPVswmf4Bj_Kqbt6SAPFTOCk<br></b>";
    echo "<b>".$_COOKIE["token"] . "</b>    :is cookie token<br>";

  //***********NOT WORKING REFESH COKIEE TOKEN***********
    $client->refreshToken($_COOKIE["token"]);//update token

    //Each time you need the access token, check if there is something saved in the cookie.
    //If $cookie is empty, you are requested to get a new acces and refresh token by authenticating.
    //If $cookie is not empty, you will tell the client to refresh the token for further use,
    // hence get a new acces token with the help of the refresh token without authenticating

    $cookie = $this->Cookie->read('token');
    //echo $cookie;
    if(!empty($cookie))
    {
//        $client->refreshToken($this->Cookie->read('token'));
//        $client->refreshToken($this->);
    }

}
if ($client->getAccessToken()) 
{
$_SESSION['token'] = $client->getAccessToken();
$session_token=$_SESSION['token'];
//ECHO "<b>".$session_token."</b><BR> is a session token";//this gives you values of "access_token" , "token_type",expires_in","refresh_token" and "created"
} 
else
{
  $authUrl = $client->createAuthUrl();
  print "<a class='login' href='$authUrl'>Connect Me!</a>";
}

insertEvent("test 123 test Google API for send calender for Without sending Notification",$cal,'Meeting room 1','phularipriti@gmail.com');

function insertEvent($subject='123 Test for add calender using Google API',$cal,$location='Somewhere',$email='priti.phulari@gslab.com')
{
echo "<br>in insert event function <br>";
$event = new Google_Event();
$event->setSummary($subject);
$event->setColorId(4); // it gives color to event
$event->setLocation($location);
$start = new Google_EventDateTime();
$start->setTimeZone('Asia/Kolkata');
$start->setDateTime('2014-02-21T19:00:00.000+05:30');
$event->setStart($start);
$end = new Google_EventDateTime();
$end->setDateTime('2014-02-21T19:25:00.000+05:30');
$event->setEnd($end);
$event->setDescription('I like this Event simply awesome');
$attendee1 = new Google_EventAttendee();
$attendee1->setEmail($email);
$attendees = array($attendee1); 
$event->setICalUID ='20140402';            
$event->setId  ='20140402';               
$event->attendees = $attendees;
$event->setTransparency("opaque");
$optinalArguments = array("sendNotifications"=>true);

$createdEvent = $cal->events->insert('primary',$event,$optinalArguments);

print_r ($createdEvent);
echo "Uid for the calender - ".$createdEvent['id']; //Note this for delete or update event
//echo "Send Notification";
}

?>

I have written line in the code where it get stucks.
**********************before refresh it gives me value but after use of refreshToken it do not give**********

AM not getting why it is not working.
(I think, )Syntax (and everything) is correct!
then why ???

1
Contributor
1
Reply
5
Views
3 Years
Discussion Span
Last Post by Priti_P
0

Hello,
So silly I was,
I was passing whole $_SESSION['token']
not the actual token.
I decode it using json and sent refreshToken key.
it is working but now the another problem is,

what after cookie expire?
how to handle it?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.