3
votes

I am trying to use the Google Calendar v3 API with php (v2) client library and a google service account to get a list of available calendars but all my attempts result in a status code 400 "invalid_grant", "bad request" error.

I created the service account, downloaded the json file and shared the calendar with the service account client_email.

here is the code I'm using:

<?php
require_once 'google-api/vendor/autoload.php';

getCalendars();


function getClient()
{
    $key_file_location = '../keys/serviceaccount.json';

    $client    = new Google_Client();

    $client->setApplicationName("Calendar test");
    $client->setAuthConfig($key_file_location);
    $client->setScopes(['https://www.googleapis.com/auth/calendar.readonly','https://www.googleapis.com/auth/calendar']);

    return $client;
}

function getCalendars()
{
    $client         = getClient();

    $calsInfo       = array();
    $service        = new Google_Service_Calendar($client);

    $calendarList   = $service->calendarList->listCalendarList();

    while(true) {
      foreach ($calendarList->getItems() as $calendarListEntry) {
        $calObj                 = new stdClass();

        $calObj->id             = $calendarListEntry->getId();
        $calObj->summary        = $calendarListEntry->getSummary();
        $calObj->description    = $calendarListEntry->getDescription();

        array_push($calsInfo, $calObj);
      }

      $pageToken = $calendarList->getNextPageToken();

      if ($pageToken) {
        $optParams      = array('pageToken' => $pageToken);
        $calendarList   = $service->calendarList->listCalendarList($optParams);
      } else {
        break;
      }
    }
    echo '################ start calendar list: ##################<br />';
    echo '<pre>'; print_r($calsInfo); echo '</pre>';
    echo '################ end calendar list: ####################<br />';
}
?>

Every time I run the script I get the following error:

[:error] [pid 214] [client ::1:51761] PHP Fatal error:  Uncaught exception 'Google_Service_Exception' with message '{\n "error": "invalid_grant",\n "error_description": "Bad Request"\n}\n' in /Users/kona/Sites/google/google-api/src/Google/Http/REST.php:134\nStack trace:\n#0 /Users/kona/Sites/google/google-api/src/Google/Http/REST.php(92): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\\Psr7\\Response), Object(GuzzleHttp\\Psr7\\Request), 'Google_Service_...')\n#1 [internal function]: Google_Http_REST::doExecute(Object(GuzzleHttp\\Client), Object(GuzzleHttp\\Psr7\\Request), 'Google_Service_...')\n#2 /Users/kona/Sites/google/google-api/src/Google/Task/Runner.php(181): call_user_func_array(Array, Array)\n#3 /Users/kona/Sites/google/google-api/src/Google/Http/REST.php(57): Google_Task_Runner->run()\n#4 /Users/kona/Sites/google/google-api/src/Google/Client.php(758): Google_Http_REST::execute(Object(GuzzleHttp\\Client), Object(GuzzleHttp\\Psr7\\Request), 'Google_Service_...', Array)\n#5 /Users/kona/Sites/google/google-api/src/Google/Service/Resource.php(232): Google_Client->execute(O in /Users/kona/Sites/google/google-api/src/Google/Http/REST.php on line 134

is anyone able to shed some light on the error?

1

1 Answers

0
votes

It appears that I had a time sync issue with my local server. Updated the server's clock and all is well.