简体   繁体   中英

Google Calendar API v3 Insert Event

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM