I’m trying to add a new appointment to my Google Calendar using a service account but keep getting a 403 Forbidden error. I can successfully retrieve and display all my calendars, so the connection works fine. The problem only happens when I try to create new events.
require_once 'vendor/Google_Client.php';
require_once 'vendor/contrib/Google_CalendarService.php';
session_start();
const APP_CLIENT_ID = 'YYYYYYYY.apps.googleusercontent.com';
const SERVICE_EMAIL = '[email protected]';
const PRIVATE_KEY_PATH = 'keys/YYYYYYYY-privatekey.p12';
$googleClient = new Google_Client();
$googleClient->setApplicationName("MyCalendarApp");
if (isset($_SESSION['access_token'])) {
$googleClient->setAccessToken($_SESSION['access_token']);
}
$privateKey = file_get_contents(PRIVATE_KEY_PATH);
$googleClient->setClientId(APP_CLIENT_ID);
$googleClient->setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_EMAIL,
'https://www.google.com/calendar/feeds/TARGET_CALENDAR_ID/private/full/',
$privateKey)
);
$calendarService = new Google_CalendarService($googleClient);
$newEvent = new Google_Event();
$newEvent->setSummary('Morning Meeting');
$newEvent->setLocation('Conference Room A');
$startTime = new Google_EventDateTime();
$startTime->setDateTime('2012-07-15T09:00:00.000-07:00');
$newEvent->setStart($startTime);
$endTime = new Google_EventDateTime();
$endTime->setDateTime('2012-07-15T10:00:00.000-07:00');
$newEvent->setEnd($endTime);
$result = $calendarService->events->insert(MY_CALENDAR_ID, $newEvent);
The error I get is just:
(403) Forbidden
My calendar is set to public. Does anyone know what might be causing this? Maybe I need different permissions or scopes? I’ve been searching everywhere but can’t figure out where to configure write access for events.