iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐙

How to Fix Missing Events When Fetching from Google Calendar API

に公開

What Happened

I was using the following code to retrieve events from Google Calendar.

import 'package:googleapis/calendar/v3.dart' as v3;
...
final calId = '<Calendar ID>';
v3.CalendarApi(client).events.list(
      calId,
    );

However, after a certain point, events that I added or edited could no longer be retrieved with this code.
Naturally, I couldn't have hit the API limit (1,000,000 queries/day), and since I was only fetching about 10 events, I had no idea why this was happening.

Google Calendar API Usage Limits | Google Developers

Solution

According to the following StackOverflow post, the issue was resolved by specifying the argument maxResults=9999.
Google calendar API do not return events list - Stack Overflow

When I tried it as follows, I was indeed able to retrieve all events.

import 'package:googleapis/calendar/v3.dart' as v3;
...
final calId = '<Calendar ID>';
v3.CalendarApi(client).events.list(
    calId,
    maxResults: 9999,
);

Regarding maxResults, the official page states the following:

Maximum number of events returned on one result page. The number of events in the resulting page may be less than this value, or none at all, even if there are more events matching the query. Incomplete pages can be detected by a non-empty nextPageToken field in the response. By default the value is 250 events. The page size can never be larger than 2500 events. Optional.

[Translation] The maximum number of events returned on a single result page. The number of events on the result page may be fewer than this value, or there may be none at all, even if there are more events matching the query. Incomplete pages are detected when the nextPageToken field in the response is not empty. By default, the value is 250 events. The page size can never be larger than 2500 events. Optional.

Before specifying maxResults,

  • Total events on the calendar: 50
  • Events successfully retrieved: 10

It was something like that, so I don't think there were even 250 events, but I'm glad it's resolved for now.

References

Events: list | Calendar API | Google Developers
Google calendar API do not return events list - Stack Overflow

Discussion