My goal is to insert events to a group calendar VIA PHP
Here is my code:
class Googl_Cal{
public function __construct(){
$this->client = $this->get_client();
}
public function get_client()
{
putenv('GOOGLE_APPLICATION_CREDENTIALS=/' . dirname( __FILE__ ) . '/gcal_service_account.json');
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$scopes = [ Google_Service_Calendar::CALENDAR ];
$client->setScopes($scopes);
return $client;
}
public function create_event(){
$service = new Google_Service_Calendar( $this->client );
$calendarList = $service->calendarList->listCalendarList();
$calendarId = '';
$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' => '2018-01-31T09:00:00-07:00',
'timeZone' => 'Asia/Jerusalem',
),
'end' => array(
'dateTime' => '2018-01-31T17:00:00-07:00',
'timeZone' => 'Asia/Jerusalem',
),
'recurrence' => array(
'RRULE:FREQ=DAILY;COUNT=2'
),
'reminders' => array(
'useDefault' => FALSE,
'overrides' => array(
array('method' => 'email', 'minutes' => 24 * 60),
array('method' => 'popup', 'minutes' => 10),
),
),
)
);
$calendarId = 'primary';
$event = $service->events->insert($calendarId, $event);
print_r($event);
die();
}
}
After calling create_event(); Im getting an empty list on $calendarList
Remember that a service account is not you you will be inserting into the service accounts calendar.
I suspect part of your issue is that you are not inserting it into a calendar and are probably getting an error.
$calendarId = '';
I sugest you do
$calendarId = 'primary';
This will insert into the service accounts primary calendar.
Assuming you want it writing to one of your personal calendars. Take the service account email address and share the calendar with it in the Web google calendar app. It will then be able to write to it. Tip grab the calendar id out of the web site while you are at it as calendarlist.list isnt going to return it until you add it, this is an annoying feature in service accounts.