creating google calendar event - php

am trying to create google calendar event using the following code given below, but am getting class Event not found . How to create a new event. please help
<?php
require_once '../../src/Google_Client.php';
require_once '../../src/contrib/Google_CalendarService.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("Google Calendar PHP Starter Application");
$client->setClientId('');
$client->setClientSecret('');
$client->setRedirectUri('simple.php');
$client->setDeveloperKey('insert_your_developer_key');
$cal = new Google_CalendarService($client);
if (isset($_GET['logout'])) {
unset($_SESSION['token']);
}
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
$authUrl = $client->createAuthUrl();
if (!$client->getAccessToken()) {
here am creating the new event
$event = new Event();
$event->setSummary("test title");
$event->setLocation("test location");
$start = new EventDateTime();
$start->setDateTime('04-03-2012 09:25:00:000 -05:00');
$event->setStart($start);
$end = new EventDateTime();
$end->setDateTime('04-03-2012 10:25:00:000 -05:00');
$createdEvent = $cal->events->insert('primary', $event);
echo $createdEvent->getId();
}

I had the exact same problem. Their documentation very incomplete. The class names are wrong in their example. Here is the code I got to work:
$event = new Google_Event();
$event->setSummary('Halloween');
$event->setLocation('The Neighbourhood');
$start = new Google_EventDateTime();
$start->setDateTime('2012-10-31T10:00:00.000-05:00');
$event->setStart($start);
$end = new Google_EventDateTime();
$end->setDateTime('2012-10-31T10:25:00.000-05:00');
$event->setEnd($end);
$createdEvent = $cal->events->insert('[calendar id]', $event); //Returns array not an object
echo $createdEvent->id;
$cal->events->insert returns an array, not an object like in their example code. If you want it to return an object you need to define it in your Google_Client call, like so:
$client = new Google_Client(array('use_objects' => true));

I had this problem too, and it seems things have changed with Google's API again.
Using the code example in the docs I was trying do
$calendar = new Calendar();
This threw an error for class Calendar not found. Turns out everything was renamed and I had to do the following:
$calendar = new Google_Service_Calendar_Calendar();
Seems like an ok naming convention to avoid conflicts, but it would have been nice to update the docs. So to solve OP's problem today he would now solve his issue using:
$event = new Google_Service_Calendar_Event();
You can look through the file Google/Service/Calendar.php and you should see all the relevant classes named with this naming convention.

Related

Google API / Calendar : can't find a way to use google api

I'm trying to use Google calendar api and its php client library to make a php function that add an event with the variables i give it through parameters.
But it seems that the documentation is outdated, and i can't find a good tutorial to help me. Here is what I've done for the moment :
<?php
require_once 'google-api-php-client/autoload.php';
session_start();
//require_once 'google-api-php-client/src/Google/Client.php';
//require_once 'google-api-php-client/src/Google/Service/Calendar.php';
$client = new Google_Client();
$client->setApplicationName("test");
$client->setClientId("xxxx.apps.googleusercontent.com");
$client->setClientSecret("xxxx");
$client->setRedirectUri("http://localhost/");
$client->setDeveloperKey("xxxx");
$service = new Google_Service_Calendar($client);
$event = new Google_Service_Calendar_Event();
$event->setSummary('Appointment');
$event->setLocation('Somewhere');
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime('2014-10-16T10:00:00.000-07:00');
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime('2014-10-16T10:25:00.000-07:00');
$event->setEnd($end);
/*$attendee1 = new EventAttendee();
$attendee1->setEmail('attendeeEmail');
$attendees = array($attendee1,
// ...
);
$event->attendees = $attendees;*/
$createdEvent = $service->events->insert('primary', $event);
echo $createdEvent->getId();
?>
And my browser tells me :
Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling POST https://www.googleapis.com/calendar/v3/calendars/primary/events?key=xxxx: (401) Login Required' in C:\wamp\www\cnsi\google-api-php-client\src\Google\Http\REST.php on line 76
Could someone help me please ?
I recommend that you used libraries of Zend Framework for it.
checkout http://framework.zend.com/manual/1.12/en/zend.gdata.calendar.html

Trying to create event in google calendar

i was started working on google calendar recently. i gone through their documentation but it was not clear.
The code following is to insert event into users calendar.
<?php
require_once dirname(__FILE__).'/Google/Client.php';
require_once dirname(__FILE__).'/Google/Service/Analytics.php';
require_once dirname(__FILE__).'/Google/Service/Calendar.php';
$scriptUri = "http://".$_SERVER["HTTP_HOST"].$_SERVER['PHP_SELF'];
$client = new Google_Client();
$client->setAccessType('online');
$client->setApplicationName('MYAPP');
$client->setClientId('XXXXXXXXXXXX');
$client->setClientSecret('XXXXXXXXXXXX');
$client->setRedirectUri($scriptUri);
$client->setDeveloperKey('XXXXXXXXXXXXXXXXX'); // API key
$scopes = array('https://www.googleapis.com/auth/calendar');
$client->setScopes($scopes);
$cal = new \Google_Service_Calendar($client);
if (isset($_GET['logout'])) { // logout: destroy token
unset($_SESSION['token']);
die('Logged out.');
}
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
}
if (isset($_SESSION['token'])) { // extract token from session and configure client
$token = $_SESSION['token'];
$client->setAccessToken($token);
}
if (!$client->getAccessToken()) { // auth call to google
$authUrl = $client->createAuthUrl();
header("Location: ".$authUrl);
die;
}
$event = new Google_Event();
$event->setSummary('Halloween');
$event->setLocation('The Neighbourhood');
$start = new Google_EventDateTime();
$start->setDateTime('2012-10-31T10:00:00.000-05:00');
$event->setStart($start);
$end = new Google_EventDateTime();
$end->setDateTime('2012-10-31T10:25:00.000-05:00');
$event->setEnd($end);
$createdEvent = $cal->events->insert("xxxxxx#gmail.com", $event); //Returns array not an object
echo $createdEvent->id;
But am getting fatal error like
Class 'Google_Event' not found
Please help with this issue. i searched in Stack overflow for the same problem . i didn't get any solution. any type of help is appreciated
I just found the solution for myself.
It turns out that they changed all the names again.
Replace Google by Google_Service_Calendar_
Eg:
$event = new Google_Service_Calendar_Event();
becomes now :
$event = new Google_Service_Calendar_Event();
It will work !
I have no working code as I opted for the Service Account method.
Maybe you can try removing the
" $client->setDeveloperKey('XXXXXXXXXXXXXXXXX'); " which often gives error.
You can also modify your config.php file and opt for offline mode. (see documentation).
Finally you can refresh token with this code :
if($client->isAccessTokenExpired()) {
$client->authenticate();
$NewAccessToken = json_decode($client->getAccessToken());
$client->refreshToken($NewAccessToken->refresh_token);
}
Finally, you can logout, delete cache so that you get a new token. Hope it helps

Google calendar API- Invitation email doesn't send to attendees when create event

I want to add event to google calendar using google api.
But after event created, invitation email doesn't send to attendees email list.
Here is my code:
<?php
require_once '../../src/Google_Client.php';
require_once '../../src/contrib/Google_CalendarService.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("Google Calendar PHP Starter Application");
// Visit https://code.google.com/apis/console?api=calendar to generate your
// client id, client secret, and to register your redirect uri.
$client->setClientId('309388785502.apps.googleusercontent.com');
$client->setClientSecret('hvJQUDYz4rY0HiYcgS46yxB-');
$client->setRedirectUri('http://localhost/GoogleApi/google-api-php-client/examples/calendar/simple.php');
$client->setDeveloperKey('AIzaSyAbBRxRKM9mkXKA17Bruul6lCq-vhR6gqc');
$cal = new Google_CalendarService($client);
if (isset($_GET['logout'])) {
unset($_SESSION['token']);
}
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if ($client->getAccessToken()) {
$event = new Google_Event();
$event->setSummary('Halloween3');
$event->setLocation('The Neighbourhood');
$start = new Google_EventDateTime();
$start->setDate('2013-10-3');
$event->setStart($start);
$end = new Google_EventDateTime();
$end->setDate('2013-10-3');
$event->setEnd($end);
$event->sendNotifications=true;
$event->maxAttendees=2;
$attendee1 = new Google_EventAttendee();
$attendee2 = new Google_EventAttendee();
$attendee1->setEmail("cuong***.ictu#gmail.com");
$attendee2->setEmail("webtr***#gmail.com");
$attendees = array($attendee1,$attendee2);
$event->attendees = $attendees;
$createdEvent = $cal->events->insert('primary', $event);
$_SESSION['token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Connect Me!</a>";
}
?>
So my question is how can i send email to attendees list after event created from API?
Thank you.
the insert method has an optional parameter:
/**
* Creates an event. (events.insert)
*
* #param string $calendarId Calendar identifier.
* #param Google_Event $postBody
* #param array $optParams Optional parameters.
*
* #opt_param int maxAttendees The maximum number of attendees to include in the response. If there are more than the specified number of attendees, only the participant is returned. Optional.
* #opt_param bool sendNotifications Whether to send notifications about the creation of the new event. Optional. The default is False.
* #return Google_Event
*/
So I resolve in this way:
[... ]$event->attendees = $attendees;
$optionaArguments = array("sendNotifications"=>true);
$createdEvent = $cal->events->insert($idCalendario, $event, $optionaArguments);
[...]
Now the attendees receive an email with a .ics file like a normal invitation from Google Calendar
In the newer API version, you add a parameter called sendUpdates="all":
https://developers.google.com/calendar/v3/reference/events/insert
So something like:
$optionalArguments = array("sendUpdates"=>"all");
$createdEvent = $cal->events->insert($idCalendar, $event, $optionalArguments);

No events being shown when retrieving events using google calendar

I am encountering a problem with my code when I am trying to retrieve events on the google calendar. Below is my code:
<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_CalendarService.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("Google Calendar PHP Starter Application");
// Visit https://code.google.com/apis/console?api=calendar to generate your
// client id, client secret, and to register your redirect uri.
$client->setClientId('client id');
$client->setClientSecret('client secret');
$client->setRedirectUri('redirect uri');
$client->setDeveloperKey('developer key');
$cal = new Google_CalendarService($client);
$events = $cal->events->listEvents('email address for google calendar');
echo $events;
?>
When I print out the events of my google calendar, all I get is an empty array. However I am sure that I have several events in my google calendar.
In fact, I event tried to add an event by using the code below:
$event = new Google_Event();
$event->setSummary('Appointment');
$event->setLocation('Somewhere');
$start = new Google_EventDateTime();
$start->setDateTime('2013-08-23T10:00:00.000-07:00');
$event->setStart($start);
$end = new Google_EventDateTime();
$end->setDateTime('2013-08-23T10:25:00.000-07:00');
$event->setEnd($end);
echo $events;
$createdEvent = $cal->events->insert('email address for google calendar', $event);
However, when I do this code I end up receiving an error that says:
Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error calling POST https://www.googleapis.com/calendar/v3/calendars/'email address for google calendar'/events?key='key'......................................lib/google-api-php-client/src/io/Google_REST.php on line 66
Can someone help me on this?
As it's an array you need to use print_r instead of echo. Try replacing:
echo $events;
with
print"<pre>".print_r($events, true)."</pre>";

Creating an google calendar event with gadget using google-api-php-client

I have a working script which is creating events in my google calendar - using google-api-php-client. I'm trying to add a gadget to each event it creates with the following code:
require_once "google-api-php-client/src/Google_Client.php";
require_once "google-api-php-client/src/contrib/Google_CalendarService.php";
define('CLIENT_ID', 'xxx...');
define('SERVICE_ACCOUNT_NAME', 'xxx...');
define('KEY_FILE', 'xxx...');
$client = new Google_Client();
$client->setApplicationName("WEB ACCTION");
$key = file_get_contents(KEY_FILE);
$client->setAssertionCredentials(new Google_AssertionCredentials(SERVICE_ACCOUNT_NAME,'xxx...',$key));
$client->setClientId(CLIENT_ID);
$service = new Google_CalendarService($client);
$event = new Google_Event();
$event->setSummary($variable1);
$event->setLocation($variable2 . ", " . $variable3);
$start = new Google_EventDateTime();
$start->setDateTime(date(DATE_ATOM, strtotime($variable4) + $time_offset));
$event->setStart($start);
$end = new Google_EventDateTime();
$end->setDateTime(date(DATE_ATOM, strtotime($variable5) + $time_offset));
$event->setEnd($end);
//The not working section
//$gadget = new Google_EventGadget();
//$gadget->setIconLink('xxx...');
//$gadget->setTitle('xxx...');
//$gadget->setHeight('xxx...');
//$gadget->setWidth('xxx...');
//$gadget->setLink('xxx...');
//$gadget->setType('xxx...');
//$gadget->setDisplay('xxx...');
//$event->setGadget($gadget);
$createdEvent = $service->events->insert($g_calendar , $event);
Unfortunately - I can not make it work and I was not able to find any information about creating the gadgets with google-api-php-client. The library includes a Google_EventGadget class and setGadget function but there is no information how to use ithem. Could anyone help me?
I have found the solution - it occures that the google-api-php-client supports only the certified links to the gadget and its icon. It didnĀ“t work until I used the https://www... links.
It works fine even though the hosting I used for tests has no security certificate.
$gadget->setIconLink('https://www...');
$gadget->setLink('https://www....');
$gadget->setDisplay('chip');
$gadget->setWidth('400');
$gadget->setHeight('100');
$event->setGadget($gadget);
$createdEvent = $service->events->insert($g_calendar , $event);
Hope it will save someone some time!

Categories