Getting Recurrence of API Google Calendar Events

2019-08-23 05:53发布

问题:

Good morning,

I'm creating a Calendar that syncronizes with Google Calendar, but I can´t get the recurrence of the Events of Google Calendar. I think the field "recurrence" of the events is a protected field. But I don't know how to get the field of a saved Event.

EDIT: The code:

$params = array(
                'orderBy' => 'startTime',
                'singleEvents' => 'true',
                'timeMin' => date(DateTime::ATOM),
        );
$listarEventos = $service->events->listEvents($calendar_id, $params);
foreach ($listarEventos['items'] as $i){
     echo $i->recurrence;
}

And If I try to print the content of the events, it shows me an empty field recurrence, and I have proved that it's recurrent.

Thanks!

回答1:

Events.list returns a list of events resources.

"kind": "calendar#event",
   "etag": "\"9722590000\"",
   "id": "3nmhbd0465ts1enliigj4",
   "status": "confirmed",
   "htmlLink": "https://www.google.com/calendar/event?eid=285781",
   "created": "2015-04-13T07:24:21.000Z",
   "updated": "2015-04-13T07:24:21.509Z",
   "summary": "My event",
   "location": " ",
   "creator": {
    "email": "XXX@gmail.com",
    "displayName": "XXX",
    "self": true
   },
   "organizer": {
    "email": "XXX@gmail.com",
    "displayName": "XXX",
    "self": true
   },
   "start": {
    "dateTime": "2015-04-21T19:00:00+02:00",
    "timeZone": "Europe/Copenhagen"
   },
   "end": {
    "dateTime": "2015-04-21T21:00:00+02:00",
    "timeZone": "Europe/Copenhagen"
   },
   "recurrence": [
    "RRULE:FREQ=WEEKLY;COUNT=24;BYDAY=TU"
   ],
   "iCalUID": "XXX@google.com",
   "sequence": 0,
   "reminders": {
    "useDefault": false,
    "overrides": [
     {
      "method": "popup",
      "minutes": 45
     }
    ]
   }

If the event has a recurrent set you can find it in there.

"recurrence": [ "RRULE:FREQ=WEEKLY;COUNT=24;BYDAY=TU" ]

I cant see your code how you are accessing it but if you test at the bottom of the event.list you will notice that if you have access to the calendar you should have access to the recurrence field.



回答2:

If the current event is not the original event which owns the recurrence rules, it doesn't contain its own copy of the 'recurrence' field. Instead there will be a reference to the original event in the 'recurringEventId' field.

If you then fetch that referenced event, you will find the recurrence rules.

E.g. (using the Python API) ...

...
events = service.events().list(calendarId=calId,
                               singleEvents=True,  # expand recurrence events
                               q=searchString
                              ).execute()
for e in events['items']:
    if 'recurringEventId' in e:
        root_ev = service.events().get(CalendarId=calId,
                                       eventId = e['recurringEventId']
                                      ).execute()
        # Copy the recurrence rules from the root event into our own:
        e['recurrence'] = root_ev['recurrence']