1

I am having difficulties understanding the insertion process for the google calendar API. I have the code they gave in their example, but it is not working when I use it, the page draws a blank (if I echo text below it does not show). Here is a link to the page I am referring to: https://developers.google.com/google-apps/calendar/v3/reference/events/insert

Here is the code:

$event = new Event();
$event->setSummary('Appointment');
$event->setLocation('Somewhere');
$start = new EventDateTime();
$start->setDateTime('2011-06-03T10:00:00.000-07:00');
$event->setStart($start);
$end = new EventDateTime();
$end->setDateTime('2011-06-03T10: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();

From my understanding I am suppose to replace 'primary' with my google calendar code. I have done that, and replaced 'attendeeEmail' with the calendar owner email address, removed the extra comma, and I am not getting any response. The referencing page links to 'deprecated code' so I am unsure if what I am using is the most up to date example.

Is there a working example out there that I can reference? Or is there something wrong with what I am doing? Any advise is greatly appreciated.

Final question: This is all linked under the 'Events -> Insert' directory. There is also a 'Calendars -> Insert' and a 'CalendarList -> Insert' directory. The 'CalendarList -> Insert' says that I am to use this code to insert into a users Calendar:

$calendarListEntry = new CalendarListEntry();
$calendarListEntry->setId("calendarId");

$createdCalendarListEntry = $service->calendarList->insert($calendarListEntry);

echo $createdCalendarListEntry->getSummary();

Should I be using this instead? If so where can I find an example or tutorial to do use this? (How do I insert startDateTime, endDateTime, timeZone, etc)

SOLVED - Here is the correct code:

$event = new Google_Service_Calendar_Event();
$event->setSummary('Appointment');
$event->setLocation('Somewhere');
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime('2011-06-03T10:00:00.000-07:00');
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime('2011-06-03T10:25:00.000-07:00');
$event->setEnd($end);
$attendee1 = new Google_Service_Calendar_EventAttendee();
$attendee1->setEmail('email');
// ...
$attendees = array($attendee1
               //, ...
              );
$event->attendees = $attendees;
$createdEvent = $service->events->insert('primary', $event);
echo $createdEvent->getId();
Ryan.NSG
  • 25
  • 1
  • 4

1 Answers1

1

You can use 'primary' string directly, no need to replace it with email if you want to access the primary calendar of the logged-in user. However, before you can insert anything anywhere, you need to authenticate correctly. Please follow the getting started here: https://developers.google.com/google-apps/calendar/instantiate There is also a php code sample.

Also there are many other working samples of inserts on SO, for example here: Create Simple Google Calendar Event in PHP

As about your final question, CalendarList collection is for showing you calendars that your authenticated users added to the list of their calendars (left panel in the web UI). It does not serve for working with events in any way. For that you will need to stick to events collection.

Community
  • 1
  • 1
luc
  • 3,642
  • 1
  • 18
  • 21
  • I have the authentication working correctly and I am able to do things like list the calendars but it is this insertion part that is not working. I went through many samples and none of them have worked thus far, like in the one you linked it uses `$cal = new Google_CalendarService($client);` and I have only been able to get `$service = new Google_Service_Calendar($client);` to work. The specific part that is not working is `$event = new Event();` and I have changed it to `$event = new Google_Event();` still nothing. – Ryan.NSG Oct 06 '14 at 15:03
  • Yes, I see what you mean. When in doubt, check the source code ;) https://github.com/google/google-api-php-client/blob/master/src/Google/Service/Calendar.php new Google_Service_Calendar($client) sounds correct, for the event the name is: Google_Service_Calendar_Event – luc Oct 06 '14 at 22:22
  • Did that too xD Google_Service_Calendar worked, but the date/time wasn't but I'm sure I can figure it out from there, thank you for your help! – Ryan.NSG Oct 07 '14 at 15:28
  • Changing the email address to 'primary' was the trick to enable notifications – znat Nov 15 '15 at 20:47