Like so many others before me, I'm having great difficulty getting full functionality to a PHP script that adds/deletes/Updates Google Calendar events.
My code, from the Daimto web site works using Oauth2 in that it lists the current events in my main calendar.
However when I add some code to add an event, the page is blank and I'm unable to trap an error.
The code is:
<?php
require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php');
session_start();
// ******************************************************** //
// Get these values from https://console.developers.google.com
// Be sure to enable the Analytics API
// ******************************************************** //
$client_id = 'xxx.apps.googleusercontent.com';
$client_secret = 'xxxkQO8K';
$redirect_uri = 'xxx/GoogleCalendar/tests/MGC_NEW.php';
$client = new Google_Client();
$client->setApplicationName("primary");
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->setAccessType('offline'); // Gets us our refreshtoken
$client->setScopes(array('https://www.googleapis.com/auth/calendar'));
//For loging out.
if (isset($_GET['logout'])) {
unset($_SESSION['token']);
}
// Step 2: The user accepted your access now you need to exchange it.
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
// Step 1: The user has not authenticated we give them a link to login
if (!isset($_SESSION['token'])) {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Connect Me!</a>";
}
// Step 3: We have access we can now create our service
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
print "<a class='logout' href='http://www.marchgolfclub.co.uk?logout=1'>LogOut</a><br>";
$service = new Google_Service_Calendar($client);
$calendarList = $service->calendarList->listCalendarList();
//print_r($calendarList);
while(true) {
foreach ($calendarList->getItems() as $calendarListEntry) {
echo $calendarListEntry->getSummary()."<br>
";
// get events
$events = $service->events->listEvents($calendarListEntry->id);
foreach ($events->getItems() as $event) {
/*Code to add holiday and Bank Holidays etc.*/
//echo "-----".$event->getSummary()."<br>";
}
}
$pageToken = $calendarList->getNextPageToken();
if ($pageToken) {
$optParams = array('pageToken' => $pageToken);
$calendarList = $service->calendarList- >listCalendarList($optParams);
} else {
break;
}
}
//THIS IS WHERE THE CODE BREAKS DOWN
echo "<hr><font size=+1>I have access to your calendar</font>";
$event = new Google_Service_Calendar_Event($client);
$event->setSummary(‘Interview’);
$event->setLocation(‘Hell’);
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime(‘2015-10-03T10:00:00.000-07:00’);
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime(‘2015-10-03T10:25:00.000-07:00′);
$event->setEnd($end);
$attendee1 = new Google_Service_Calendar_EventAttendee();
$attendee1->setEmail(’email@abc.com’);
// …
$attendees = array($attendee1);
$event->attendees = $attendees;
//$createdEvent = $service->events->insert(‘primary’, $event);
//Google code error was fixed
$createdEvent = $service->events->insert($calName, $event);
//echo $createdEvent->getId();
var_dump($createdEvent);
echo "<br><font size=+1>Event created</font>";
//END OF BROKEN CODE
}
?>`
Can anyone help?