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);
Related
I must be missing something in the api docs, how do I update a Google event timezone via an api request?
$service = new Google_Service_Calendar($client);
$event = $service->events->get($calendarId, $eventId);
$event->setSummary($summary);
$service->events->update($calendarId, $event->getId(), $event);
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime($dateTimeStr);
$event->setStart($start);
$event->setTimezone('America/Los_Angeles');
The error message:
Attempted to call an undefined method named "setTimezone" of class "Google_Service_Calendar_Event". (500 Internal Server Error)
You have to set the timezone on the calendar itself. Don't set it on the event.
See the examples in the documentation:
$calendar = $service->calendars->get('primary');
$calendar->setTimeZone('America/Los_Angeles');
$updatedCalendar = $service->calendars->update('primary', $calendar);
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
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>";
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!
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.