Update Google Calendar Event TimeZone with PHP - php

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);

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

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>";

Adding a Google Calendar using apiClient

I'm finally able to get an Access Token, now I'm very confused as how to add a Calendar using only Google's provided apiClient.
$apiClient = SiteController::getApiClient();
$service = new apiCalendarService($apiClient);
$calendar = new Calendar();
$calendar->description = "What";
$service->calendars->insert($calendar);
This produces:
Error calling POST https://www.googleapis.com/calendar/v3/calendars?key=mykey: (400) Required
Is there some documentation/examples on adding a Calendar? There are a ton of examples, it seems like, for simply adding an Event.
I'm a little closer now, I get
apiServiceException
Error calling POST
https://www.googleapis.com/calendar/v3/users/me/calendarList?key=mykey: (404) Not Found
Using the boilerplate code they gave on the documentation
$calendarListEntry = new CalendarListEntry();
$calendarListEntry->setId("calendarId");
$createdCalendarListEntry = $service->calendarList->insert($calendarListEntry);
echo $createdCalendarListEntry->getSummary();
Inserting a new calendarEntry in google calendar API v3 returns a 404
How do I change my request URL from
https://www.googleapis.com/calendar/v3/users/me/calendarList?key=mykey
to
https://www.googleapis.com/calendar/v3/calendars
This worked:
// Create new calendar
$apiClient = SiteController::getApiClient();
$service = new apiCalendarService($apiClient);
$calendar = new Calendar();
$calendar->setSummary(Home::model()->count() . '-' . $model->name);
$createdCalendar = $service->calendars->insert($calendar);
You can reference Google Calendar API v1 Developers' Guide.

Categories