David Holada
David Holada

Reputation: 109

Google Calendar API error in retrieving a list of calendars

I tried to do some aplication in Google Calendar API. I do everything necessary to make retrieve a list of calendars of authentificated user according to this https://developers.google.com/google-apps/calendar/v1/developers_guide_php#RetrievingCalendars manual. I write this code

<?php
$clientLibraryPath = 'phpGoogle/library';
$oldPath = set_include_path(get_include_path() . PATH_SEPARATOR . $clientLibraryPath);
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_AuthSub');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
function getCurrentUrl()
{
return  "http" . (($_SERVER["HTTPS"] == "on") ? "s://" : "://") . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}
function getAuthSubUrl()
{
  $next = getCurrentUrl();
  $scope = 'https://www.google.com/calendar/feeds/';
  $secure = false;
  $session = true;
  return Zend_Gdata_AuthSub::getAuthSubTokenUri($next, $scope, $secure,
      $session);
}
$authSubUrl = getAuthSubUrl();
echo "<a href=\"$authSubUrl\">login to your Google account</a>"; 
if(! isset($_SESSION['sessionToken']) && isset($_GET['token'])) {
  $_SESSION['sessionToken'] =
      Zend_Gdata_AuthSub::getAuthSubSessionToken($_GET['token']);
}
$client = Zend_Gdata_AuthSub::getHttpClient($_SESSION['sessionToken']);
echo $_SESSION['sessionToken'];
function outputCalendarList($client)
{
  $gdataCal = new Zend_Gdata_Calendar($client);
  $calFeed = $gdataCal->getCalendarListFeed();
  echo '<h1>' . $calFeed->title->text . '</h1>';
  echo '<ul>';
  foreach ($calFeed as $calendar) {
    echo '<li>' . $calendar->title->text . '</li>';
  }
  echo '</ul>';
}
?> 

and after authentifiacion of user, it gives me this error Fatal error: Uncaught exception 'Zend_Gdata_App_AuthException' with message 'Token upgrade failed. Reason: Token revoked.

Token revoked.

Error 403

' in /data16/lokys/html/testGoogle/phpGoogle/library/Zend/Gdata/AuthSub.php:138 Stack trace: #0 /data16/lokys/html/testGoogle/calendar.php(26): Zend_Gdata_AuthSub::getAuthSubSessionToken('1/G3lMsmGM7mQgt...') #1 {main} thrown in /data16/lokys/html/testGoogle/phpGoogle/library/Zend/Gdata/AuthSub.php on line 138

The aplication on web is here http://lokys.cz/testGoogle/calendar.php so you can try the problem. Could you someone help me?

Thank you.

Upvotes: 0

Views: 277

Answers (1)

user2918211
user2918211

Reputation: 11

Please try adding

session_start();

at the top.

I faced the same problem too but it occurred when the page is reloaded. I think when the session was not started, the one-time token cannot be upgraded to the session token.

Hope this help.

Upvotes: 1

Related Questions