My pain with google api continues ...
I can use a script to use the update token - automatically update and keep access to the calendar alive. I can interact with the calendar, and all is well.
Now I want to switch to using the google api client for php, and yesterday it worked for me while my token was valid, but now it expired. I cannot decide how to use the update token to get a new token and continue authentication as soon as it expires in the future.
I found some links and tried them - here is my test script so far based on the simple.php example:
<?php require_once '../../src/Google_Client.php'; require_once '../../src/contrib/Google_CalendarService.php'; session_start(); $client = new Google_Client(); $client->setAccessType('offline'); $client->setApplicationName("Downs Golfmanager"); // Visit https://code.google.com/apis/console?api=calendar to generate your // client id, client secret, and to register your redirect uri. $client->setClientId('XXXXXX.apps.googleusercontent.com'); $client->setClientSecret('XXXXXX'); $client->setRedirectUri('XXXXXX'); $client->setDeveloperKey('XXXXXX'); $client->refreshToken('X/XXXXX'); $client->setUseObjects(true); $cal = new Google_CalendarService($client); if (isset($_GET['logout'])) { unset($_SESSION['token']); } if (isset($_GET['code'])) { $client->authenticate($_GET['code']); $_SESSION['token'] = $client->getAccessToken(); header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']); } if (isset($_SESSION['token'])) { $client->setAccessToken($_SESSION['token']); } if($client->isAccessTokenExpired()) { / echo 'Access Token Expired'; // Debug //$client->refreshToken($refreshToken); } if ($client->getAccessToken()) { $event = new Google_Event(); $event->setSummary('Appointment'); $event->setLocation('Somewhere'); $start = new Google_EventDateTime(); $start->setDateTime('2013-08-28T14:00:00.000+01:00'); $event->setStart($start); $end = new Google_EventDateTime(); $end->setDateTime('2013-08-28T15:25:00.000+01:00'); $event->setEnd($end); $createdEvent = $cal->events->insert(' XXXXXX@group.calendar.google.com ', $event); //echo "<pre>" . print_r($createdEvent, true) . "</pre>"; echo $createdEvent->getId(); //test pulling events $events = $cal->events->listEvents(' XXXXXX@group.calendar.google.com '); while(true) { foreach ($events->getItems() as $event) { echo $event->getSummary(); } $pageToken = $events->getNextPageToken(); if ($pageToken) { $optParams = array( 'pageToken' => $pageToken, 'timeMin'=>'2013-08-26T00:00:00+01:00', 'timeMax'=>'2013-08-31T00:00:00+01:00' ); $events = $cal->events->listEvents(' XXXXXX@group.calendar.google.com ', $optParams); } else { break; } } //print "<h1>Events List</h1><pre>" . print_r($events, true) . "</pre>"; $calList = $cal->calendarList->listCalendarList(); print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>"; $_SESSION['token'] = $client->getAccessToken(); } else { $authUrl = $client->createAuthUrl(); print "<a class='login' href='$authUrl'>Connect Me!</a>"; }
As in my previous posts, I do not understand what is going to happen here. My token has expired - I have an update token. what changes in my code do I need in order to automatically update authorization at every access.
Edit: error code: The OAuth 2.0 access token has expired, and a refresh token is not available
I commented on a few extra lines, over which I included my attempts to solve, just try to avoid confusion further
thanks
source share