user1684850
user1684850

Reputation: 41

Google Calendar API v3 - Google_AuthException - Could not json decode the token issue

I've searched this Group, plus others, along with other websites and cannot find a solution to this. Error output is below. I definitely know that setAccessToken should NOT be NULL. Any guidance would be great here. the Google Calendar v3 API documentation is not so great...in fact, the samples are for older API versions.

PHP Fatal error: Uncaught exception 'Google_AuthException' with message 'Could not json decode the token' in google-api-php-client/src/auth/Google_OAuth2.php:162 Stack trace:

0 google-api-php-client/src/Google_Client.php(170): Google_OAuth2->setAccessToken(NULL)

1 Cal.php(16): Google_Client->setAccessToken(true)

2 {main} thrown in google-api-php-client/src/auth/Google_OAuth2.php on line 162

Below is the code for my app:

<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_CalendarService.php';
session_start();
$client = new Google_Client();
$service = new Google_CalendarService($client);
if (isset($_REQUEST['logout'])) {
  unset($_SESSION['token']);
}
if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
} else {
  $client->setAccessToken($client->authenticate($_GET['code']));
  $_SESSION['token'] = $client->getAccessToken();
}
if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}
if ($client->getAccessToken()) { 
$event = new Google_Event();
$event->setSummary('Appointment');
$event->setLocation('Somewhere');
$start = new Google_EventDateTime();
$start->setDateTime('2013-10-05T10:00:00.000-07:00');
$event->setStart($start);
$end = new Google_EventDateTime();
$end->setDateTime('2013-10-05T10:25:00.000-07:00');
$event->setEnd($end);
$attendee1 = new Google_EventAttendee();
$attendee1->setEmail('[email protected]');
// ...
$attendees = array($attendee1);
$event->attendees = $attendees;
$createdEvent = $service->events->insert('primary', $event);
echo $createdEvent->getId();
} else {
echo "failed hard";
}
?>

ClientID, Key, etc are kept in my google-api-php-client/src/config.php file

Upvotes: 1

Views: 1845

Answers (1)

Rob
Rob

Reputation: 12872

I haven't reviewed the api docs in a while but I use something like the following to configure the client. Maybe it will help.

$certFile = file_get_contents('/path/to/cert.p12');

$client = new Google_Client();
$client->setApplicationName('My App');
$client->setClientId($clientId);
$client->setScopes([
    'https://www.googleapis.com/auth/calendar',
    'https://www.googleapis.com/auth/calendar.readonly',
]);

if ($token = $_SESSION['google.calendar.token']) {
    $client->setAccessToken($token);
}

$credentials = new Google_AssertionCredentials($service_email, $client->getScopes(), $certFile);
$client->setAssertionCredentials($credentials);
$service = new Google_CalendarService($client);

Upvotes: 0

Related Questions