3
votes

The Events: list is not returning all the events.

I see a lot of events on the calendar, but when I call Events: list, I get only some of them.

It's not a matter of maxResults being too low. I have it set to return 2500, the max. I get 1251 or so, I deleted about 24 via the API, and now it returns 1227. However there are a bunch on the calendar that aren't coming back.

It's not a matter of the page token either. I'm looping to see if somehow the rest are on the next pageToken list, but when I call getNextPageToken after the first page, it's null.

The optional parameters I'm using is:

    $optParams = array(
        'maxResults' => 2500,
        'singleEvents' => TRUE,
        'pageToken' => $pageToken,
    );

If I set singleEvents to be false, or remove it, Google responds with an error.

singleEvents boolean Whether to expand recurring events into instances and only return single one-off events and instances of recurring events, but not the underlying recurring events themselves. Optional. The default is False.

This seemed to start happening about when Google changed the GMail name to G suites.

My code is below: (when I var_dump $calendarList, I get only 1 calendar. I used similar code to add the calendar events in the first place.)

$calendarList  = $service->calendarList->listCalendarList();
$calendarListEntry = $service->calendarList->get('primary');

$continue = 1;
echo "reading the calendar for duplicate appointments<br>";
while ($continue == 1) {
    $optParams = array(
        'maxResults' => 2500,
        'singleEvents' => TRUE,
        'pageToken' => $pageToken,
    );

    $events = $service->events->listEvents($calendarListEntry->id, $optParams);
    foreach ($events->getItems() as $event) {
        //process events
    }
    $pageToken = $events->getNextPageToken();
    if ($pageToken == null) { continue = 0; }
}

Edit: weirdly, when I lowered the maxResults to 250, I got a few that hadn't shown up in the result originally. But I'm still not getting all of them.

Edit2: forgot to include the call to read the next page token.

Edit 3: Weirdly, although I'm passing in 'singleEvents' => TRUE, I'm also getting events that are marked to repeat.

1
what are you putting in as $pageToken and why? why not just do $events = $service->events->listEvents($calendarListEntry->id) ?DaImTo
Because if I just do listEvents, it will only return the first 250. I'm putting in null initially as the pagetoken, and on subsequent calls I'm using the value I get back from $pageToken = $events->getNextPageToken(); I just forgot to add that to the code above.jim
OK and you don't want to paginate I guess. what happends if you remove the singleevents and pagetoken lines then?DaImTo
I AM paginating. Did you not read my comment or see my edit?jim

1 Answers

1
votes

With pagination

$events = $service->events->listEvents('primary');

while(true) {
  foreach ($events->getItems() as $event) {
    echo $event->getSummary();
  }
  $pageToken = $events->getNextPageToken();
  if ($pageToken) {
    $optParams = array('pageToken' => $pageToken);
    $events = $service->events->listEvents('primary', $optParams);
  } else {
    break;
  }
}