I am working on a script to pull an event from Google Calendar and put it on a website. The purpose is to be able to put the operating hours for my employer (a university library) on our website. Basically we create events in Google Calendar with the operating hours in the summary. My aim is to pull the relevant information from the Calendar programmatically and put it into a div tag on our home page.
It was not difficult to modify the basic PHP example from Google to retrieve the requisite information. Unfortunately the controller code stopped retrieving newly created events. It is not a permissions issue since I am the owner of the calendar. And up until yesterday afteoon I was able create new events and have the script print them to my browser. Here is my code:
<?php
require __DIR__ . '/vendor/autoload.php';
define('APPLICATION_NAME', 'Library Hours Script');
define('CREDENTIALS_PATH', '~/path-to-credentials');
define('CLIENT_SECRET_PATH', __DIR__ . '/private key file');
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/calendar-php-quickstart.json
define('SCOPES', implode(' ', array(
Google_Service_Calendar::CALENDAR_READONLY)
));
/**
* Retus an authorized API client.
* @retu Google_Client the authorized client object
*/
$client_email = 'project-name@clean-mason-95113.iam.gserviceaccount.com';
$private_key = file_get_contents('/path-to-private-key');
$credentials = new Google_Auth_AssertionCredentials(
$client_email,
SCOPES,
$private_key
);
// Get the API client and construct the service object.
$client = new Google_Client();
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion();
}
$service = new Google_Service_Calendar($client);
// Get the calendar ID. The script currently uses the "Regular Hours" calendar.
$calendarId = 'dsulibraryinfo@gmail.com';
$optParams = array(
'maxResults' => 1,
'singleEvents' => TRUE,
'timeMin' => date('c'),
);
$results = $service->events->listEvents($calendarId, $optParams);
if (count($results->getItems()) == 0) {
print "<a href='http://www.deltastate.edu/academics/libraries/libraries-hours-of-operation/' target='_blank'>Click here for Library hours</a>.n";
} else {
foreach ($results->getItems() as $event) {
$opHours = $event->getSummary();
$start = $event->start->dateTime;
if (empty($start)) {
$start = $event->start->date;
}
print($opHours . " " . $start . " " . date('c'));
}
}
Essentially the code above makes a request to the Calendar API for a list of events with the maximum number of results being 1, filtered by the current date. Then it prints the start date and summary to the screen.
The API explorer retus the same results as when I run the script on a web server. It only shows events in the future created some time ago. The events I created yesterday and today for later this week are not retued. The problem does not seem to be very common, but no one who has reported it has found a solution. Any help would be greatly appreciated.
