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

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

Related

Error calling POST (404 Not Found) Google Calendar API

I am trying to insert an event with Google Calendar API and PHP. Everytime I submit my form I get the below error message:
Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error calling POST https://www.googleapis.com/calendar/v3/calendars/{{MY-CALENDAR-ID-IS-HERE}}#group.calendar.google.com/events: (404) Not Found' in /Applications/MAMP/htdocs/dl-home/classes/google-api/io/Google_REST.php:66 Stack trace: #0 /Applications/MAMP/htdocs/dl-home/classes/google-api/io/Google_REST.php(36): Google_REST::decodeHttpResponse(Object(Google_HttpRequest)) #1 /Applications/MAMP/htdocs/dl-home/classes/google-api/service/Google_ServiceResource.php(186): Google_REST::execute(Object(Google_HttpRequest)) #2 /Applications/MAMP/htdocs/dl-home/classes/google-api/contrib/Google_CalendarService.php(469): Google_ServiceResource->__call('insert', Array) #3 /Applications/MAMP/htdocs/dl-home/includes/add-memo.php(66): Google_EventsServiceResource->insert('56c1bc787c21b50...', Object(Google_Event)) #4 {main} thrown in /Applications/MAMP/htdocs/dl-home/classes/google-api/io/Google_REST.php on line 66
But I dont understand what the problem is, I have my Client ID, Calendar ID and Service Email in my script, I also made sure that the sharing permissions of my calendar are shared to my service email but still no luck. I did find similar issues here and here but those havent helped. I also have review the php sample library prior to asking :)
Here is my script:
session_start();
//Call-in the google client library
require_once '../classes/google-api/Google_Client.php';
require_once '../classes/google-api/contrib/Google_CalendarService.php';
//Service and Client Acc Details
define('CLIENT_ID', '890699386256-mdpm4jr22iu6p6j4mi0bvlpgq602ri7g.apps.googleusercontent.com');
define('SERVICE_ACCOUNT_NAME', '890699386256-mdpm4jr22iu6p6j4mi0bvlpgq602ri7g#developer.gserviceaccount.com');
define('KEY_FILE', '../components/DL-Calendarapi-0a09ec997f6a.p12');
//New Session Open / Set Access Token
$client = new Google_Client();
$client->setApplicationName("Anything-here");
$client->setUseObjects(true);
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
//Set Key Cred Details
$key = file_get_contents(KEY_FILE);
$client->setClientId(CLIENT_ID);
$client->setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
'https://www.google.com/calendar/feeds/56c1bc787c21b501a73c1e7e5776560f/private/full/',
$key)
);
//Format the date to be 'friendly' with the google event input format
//This takes dates as dd/mm/yyyy and converts to yyyy-mm-dd
$df = DateTime::createFromFormat("d/m/Y", $_GET['dateFrom']);
$dt = DateTime::createFromFormat("d/m/Y", $_GET['dateTo']);
$datet = new DateTime($dt->format("Y-m-d"));
//Custom modify snippet to fix googles timezone error
$datet->modify('+1 day');
$client->setClientId(CLIENT_ID);
$cal = new Google_CalendarService($client);
//Create the event (Adding Details)
$event = new Google_Event();
//Event title set..
$event->setSummary($_GET['name']);
$event->setLocation('Anything-here-or-pass-variable');
$event->setDescription($_GET['comment']);
$start = new Google_EventDateTime();
$start->setDate($df->format("Y-m-d"));
$event->setStart($start);
$end = new Google_EventDateTime();
$end->setDate($datet->format("Y-m-d"));
$event->setEnd($end);
//Add the Event object to Calendar
$cal->events->insert('56c1bc787c21b501a73c1e7e5776560f#group.calendar.google.com', $event);
//Show success message
echo '<div id="message"><div id="message-body"><p>Event Created!</p></div></div>';
The scope seems wrong: 'https://www.google.com/calendar/feeds/56c1bc787c21b501a73c1e7e5776560f/private/full/'
For API v3 you need to use 'https://www.googleapis.com/auth/calendar'

Google Calendar API v3 PHP new event

I'm trying to do add an event. I use client libraries.
After passing oAuth I get
Fatal error: Call to a member function insert() on a non-object in zarzadzaj/kursy.php on line 17
My code is:
<?php
require_once '../zarzadzaj/src/Google_Client.php';
require_once '../zarzadzaj/src/contrib/Google_CalendarService.php';
$event = new Google_Event();
$event->setSummary('Appointment');
$event->setLocation('Somewhere');
$start = new Google_EventDateTime();
$start->setDateTime('2011-06-03T10:00:00.000-07:00');
$start->setTimeZone('America/Los_Angeles');
$event->setStart($start);
$end = new Google_EventDateTime();
$end->setDateTime('2011-06-03T10:25:00.000-07:00');
$end->setTimeZone('America/Los_Angeles');
$event->setEnd($end);
$anEvent = $service->events->insert('primary', $event);
echo $anEvent->getId();
?>
What am i doing wrong?
I found the Solution...
You need to declare '$service' as an instance of the google calendar service first before you call the insert() method..This worked for me...I hope this works for you too..
$service = new Google_CalendarService($client);

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!

creating google calendar event

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.

Categories