I'm trying to create Google Calendar events using the Google Calendar API. Specifically, I'm using the Service Account method, as my application will be running through an array of events to create and inserting them into my Google Calendar. The language being used is PHP.
I've downloaded the PHP client library from gitHub. https://github.com/googleapis/google-api-php-client.
My test code is below. This completes successfully and gives me back a long event id. However, when I try and paste this into my browser, it takes me to my calendar and says "Could not find the requested event". nothing shows on 5/28/2020 either. Can anyone please let me understand what I'm doing wrong?
The file at C:/sites/GoogleCredentials/service-account.json is my Service Account key from the Credential I created under my project in the Google Cloud Console.
require_once('google-calendar/vendor/autoload.php');
putenv('GOOGLE_APPLICATION_CREDENTIALS=C:/sites/GoogleCredentials/service-account.json');
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Google_Service_Calendar::CALENDAR_EVENTS);
$service = new Google_Service_Calendar($client);
$event = new Google_Service_Calendar_Event(array(
'summary' => 'Google I/O 2015',
'location' => '800 Howard St., San Francisco, CA 94103',
'description' => 'A chance to hear more about Google\'s developer products.',
'start' => array(
'dateTime' => '2020-05-28T09:00:00',
'timeZone' => 'America/New_York'
),
'end' => array(
'dateTime' => '2020-05-28T17:00:00',
'timeZone' => 'America/New_York'
)
));
$calendarId = 'primary';
$event = $service->events->insert($calendarId, $event);
printf('Event created: '.$event->htmlLink);
You need to remember that a service account is not you. A service account has its own google calendar account. The events are being inserted into its google calendar and not yours.
You should share your calendar with the service account using the service account email address and then use the calendar id from your calendar to insert into.
you could also leave it were is is and just make sure to invite yourself to the event run by the service account.