Search code examples
phpservicegoogle-calendar-apiservice-accounts

using service account to access google calendar but answer variables are empty


I have created a service account to access a certain google calendar. this service account user also got the permission in the calendar settings as is described in this blog: "allow my user to add meeting in my calendar with Google Calendar API with PHP without auth".

As I can see in Google Cloud Platform under APIs + Services -> Credentials in the Service Account section the created service account is used with all services (last 30 days) every time, when I fire this php script, but in the browser window I get no error, that there would be a problem with authentication but: Uncaught Error: Call to a member function getSummary() on null in line... of the php-script Script and Settings in Google Cloud Platform are as described in the mentioned post.

Any idea what could be the problem? Has there anything changed with google calendar api since this post in 2019?

require '../google-api-php-client/vendor/autoload.php';
ini_set('display_errors', 1);

$client = new Google_Client();
$client->addScope("https://www.googleapis.com/auth/calendar");

$client->setAuthConfig(dirname(__FILE__).'/credentials/credentials.json');

$service = new Google_Service_Calendar($client);

$calendarList = $service->calendarList->listCalendarList();

while(true) {

  foreach ($calendarList->getItems() as $calendarListEntry) {
    echo $calendarListEntry->getSummary();
    echo "\n------------------------------\n\n";

    // get events 
    $events = $service->events->listEvents($calendarListEntry->id);

    foreach ($events->getItems() as $event) {
        echo "- " . $event->getSummary() . "\n";
        echo "- " . $event->getStart()->getDateTime() . "\n\n";
    }

  }

  $pageToken = $calendarList->getNextPageToken();

  if ($pageToken) {
    $optParams = array('pageToken' => $pageToken);
    $calendarList = $service->calendarList->listCalendarList($optParams);
  } else {

    echo "break";
   break;
  }

}
    echo "calendar summary: " . $calendarListEntry->getSummary();
    echo "\ncalendar id: " . $calendarListEntry->getId();
    echo "\n------------------------------\n\n";

Obviously the $calendarList and $calendarListEntry is empty....


Solution

  • The reason why function getSummary() cannot render any results (is null or empty) is because a new service account user has an empty calendarList. By giving permission in any other calendar to the service account user does not automatically insert this calender to the calendarList of the service account user.

    So you first have to insert / create a calenderList for the service account:

    $calendarListEntry = new Google_Service_Calendar_CalendarListEntry();
    $calendarListEntry->setId("calendar_address_you_want_to_add");
    $createdCalendarListEntry = $service->calendarList->insert($calendarListEntry);
    echo $createdCalendarListEntry->getSummary();
    

    When using the first script now you will get results of calendarEvents (as far as there are any coming events posted in the calendar) the getSummary() function and the getId() function.