Access Google Calendar using v3 API

I am trying to rewrite an outdated application that I have not previously written using the Google Calendar v3 API.

I implemented the google-api-php client, created an API key in my developer console and went through the authentication process. I have an accessToken that I save in the database, and when it expires, I can update this token well.

However, I can’t work with calendars at all.

I keep track of what limited documentation already exists and has the following:

 $this->client = new Google_Client(); $this->client->setClientId( $this->settings['clientId'] ); $this->client->setClientSecret( $this->settings['clientSecret'] ); $this->client->setAccessToken( $this->settings['accessToken'] ); $service = new Google_Service_Calendar($this->client); $calendarList = $service->calendarList->listCalendarList(); while(true) { foreach ($calendarList->getItems() as $calendarListEntry) { echo $calendarListEntry->getSummary(); } $pageToken = $calendarList->getNextPageToken(); if ($pageToken) { $optParams = array('pageToken' => $pageToken); $calendarList = $service->calendarList->listCalendarList($optParams); } else { break; } } 

The error I am getting is:

 Message: Undefined property: Google_Service_Calendar_CalendarList::$items 

I checked my Google Calendar account and all calendars are shared, so there shouldn't be a problem, although I don’t understand why this is a problem, because I use an authenticated account anyway.

Anyone who can offer advice?

Many thanks

+5
source share
1 answer

The error you get

 Message: Undefined property: Google_Service_Calendar_CalendarList::$items 

This is because the object is empty.

Replace

 while(true) { foreach ($calendarList->getItems() as $calendarListEntry) { echo $calendarListEntry->getSummary(); }Undefined property 

with this to get rid of the error.

 if ($calendarList->getItems()) { foreach ($calendarList->getItems() as $calendarListEntry) { echo $calendarListEntry->getSummary(); } 

Also adjust the following

 $this->client = new Google_Client(); 

Must be

 $client = new Google_Client(); 
+2
source

Source: https://habr.com/ru/post/1209164/


All Articles