Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Google Calendar using API v3

I'm attempting to rewrite a legacy application which I didn't write previously using the Google Calendar v3 API.

I've implemented the google-api-php-client, created an API key in my developer console and gone through the process to authenticate. I've got an accessToken which I'm persisting in the database and when it expires, I can refresh that token fine.

However, I'm unable to any work with the Calendars at all.

I'm following what limited documentation is already out there and have 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 that I'm getting back is :

Message:  Undefined property: Google_Service_Calendar_CalendarList::$items

I've checked my Google Calendar account and all the calendars are shared, so there should be no problem, although I don't see why that is an issue since I'm using an authenticated account anyway.

Anyone able to offer advice?

Many Thanks

like image 965
madebyhippo Avatar asked Nov 01 '22 12:11

madebyhippo


1 Answers

The Error you are getting

Message:  Undefined property: Google_Service_Calendar_CalendarList::$items

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();

Needs to be

$client = new Google_Client();
like image 132
Jonny C Avatar answered Nov 11 '22 06:11

Jonny C