Target
I'm trying to integrate Google Calendar API to my company website to automate event creation upon some events.
The events will be added to a company calendar and displayed to the users, so i don't need the users to authenticate, i will not work on their own calendars.
What i've done
made a OAuth 2.0 service account as described in the Google OAuth 2.0 for Server to Server Applications docs
created, downloaded and added the credentials file to my project, and added its path to the environment variable
GOOGLE_APPLICATION_CREDENTIALS
;followed the docs example to authorize my requests using the service account and the docs to create events.
Here's a short version of my sourcecode:$client = new Google_Client(); $client->setApplicationName("XXX"); $client->addScope(Google_Service_Calendar::CALENDAR); // "https://www.googleapis.com/auth/calendar" $client->useApplicationDefaultCredentials(); $service = new Google_Service_Calendar($client); $event = new Google_Service_Calendar_Event(/* array with event data */); $calendarId = /* calendar id taken from calendar Settings and Sharing on calendar.google.com*/; $event = $service->events->insert($calendarId, $event);
Compared to the example (lines 43:52) I didn't manually check for the credential files but gone straight for
useApplicationDefaultCredentials()
.
The error
The logged errror from my code is
[2020-10-19 14:29:19] local.ERROR: {
"error": {
"errors": [
{
"domain": "global",
"reason": "notFound",
"message": "Not Found"
}
],
"code": 404,
"message": "Not Found"
}
}
{"userId":2,"exception":"[object] (Google_Service_Exception(code: 404): {
\"error\": {
\"errors\": [
{
\"domain\": \"global\",
\"reason\": \"notFound\",
\"message\": \"Not Found\"
}
],
\"code\": 404,
\"message\": \"Not Found\"
}
}
at /var/www/vendor/google/apiclient/src/Google/Http/REST.php:123)
[stacktrace]
It looks like it is calling the wrong api endpoint since a 404
is a page not found
response.
Calendar ID
Just to be clear, here is where i taken the calendar id