1
votes

I am getting events from calendar using Google Calendar PHP API.

Everything works with the initial calendar, but now I am trying to use the same setup to receive events from another calendar (same Google account).

There's a following error in the log:

PHP Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling GET https://www.googleapis.com/calendar/v3/calendars/CALENDAR_ID.calendar.google.com/events?maxResults=999&orderBy=startTime&singleEvents=true&timeZone=Europe%2FTallinn&timeMin=2016-09-19T00%3A01%3A00Z&timeMax=2018-07-17T00%3A00%3A00Z: (404) Not Found' in /www/apache/domains/www.domain.ee/htdocs/wp-content/themes/THEME/booking/vendor/google/apiclient/src/Google/Http/REST.php:79

And here's the code: (only thing I chaged is $calendarId)

require_once 'vendor/autoload.php';

$client_email = '[email protected]';
$private_key = file_get_contents('name.p12');
$scopes = array('https://www.googleapis.com/auth/calendar');
$credentials = new Google_Auth_AssertionCredentials(
    $client_email,
    $scopes,
    $private_key
);

$client = new Google_Client();
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
  $client->getAuth()->refreshTokenWithAssertion();
}

$service = new Google_Service_Calendar($client);

// Print the next 999 events on the user's calendar.
$calendarId = '[email protected]';
$optParams = array(
  'maxResults' => 999,
  'orderBy' => 'startTime',
  'singleEvents' => TRUE,
  'timeZone' => 'Europe/Tallinn',
  'timeMin' => '2016-09-19T00:01:00Z',
  'timeMax' => '2018-07-17T00:00:00Z',
);
$results = $service->events->listEvents($calendarId, $optParams);

Inserting same data into Google APIs Explorer returns exactly the events I am looking for.

1

1 Answers

2
votes

From the Handling API Errors in the Google Calendar API, the error 404 that you get sometime means that the specified resource was not found. Other possible cause is when the requested resource (with the provided ID) has never existed or when accessing a calendar that the user can not access.

The suggested action here is use exponential backoff.

Based from this SO question, another caused of this issue is when accessing private calendar with a service account, you need to either perform an authority delegation if you own the domain containing those calendars or you will need to share the private calendar with the email address of the service account.