Search code examples
phpgoogle-calendar-apiservice-accounts

Shared as usual to service account, calendar does not appear in the list when querying it from Calendar API


I have around 51 Google calendars shared to a google service account and they are still working well, I am getting push notification from them on a regular basis. I added one more calendar in the usual way with: "share to other people" and set e-mail to my service account. I then tried to get a list of all calendars that are connected to the service account I can not get the newest one. It is absent.

Here is the Joomla php code I have used for the past 2-3 years:

public function loadCalendarList() {

    $this->user = JFactory::getUser();
    if ($this->_connect()) {
        **$list = $this->_service->calendarList->listCalendarList();**
        if (!empty($list['items'])) {
....

All calendars except new one are loaded. I have search all over internet but no clue, I am not using G Suite. If I use google OAuth 2.0 authorisation without any service account I can load all 52 calendar with the new one. One more thought: I have not added to this new calendar for several months, I think something changed in google?


Solution

  • For newly shared calendars, after sharing a calendar with the service account, you need to explicitly insert it into the service account's calendar list. Something like this:

    $newCalendar=new Google_Service_Calendar_CalendarListEntry();
    $newCalendar->setId("THE ID OF THE NEWLY ADDED CALENDAR");
    $service->calendarList->insert($newCalendar);