Google 日历获取每个日历中的所有事件

Google Calendar get all events from every calendar

我想检索日历中的所有事件,然后按日历对它们进行分类。

这是我目前的情况。

public class Quickstart {
/** Application name. */
private static final String APPLICATION_NAME =
    "Google Calendar API Java Quickstart";

/** Directory to store user credentials for this application. */
private static final java.io.File DATA_STORE_DIR = new java.io.File(
    System.getProperty("user.home"), ".credentials/calendar-java-quickstart");

/** Global instance of the {@link FileDataStoreFactory}. */
private static FileDataStoreFactory DATA_STORE_FACTORY;

/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY =
    JacksonFactory.getDefaultInstance();

/** Global instance of the HTTP transport. */
private static HttpTransport HTTP_TRANSPORT;

/** Global instance of the scopes required by this quickstart.
 *
 * If modifying these scopes, delete your previously saved credentials
 * at ~/.credentials/calendar-java-quickstart
 */
private static final List<String> SCOPES =
    Arrays.asList(CalendarScopes.CALENDAR_READONLY);

static {
    try {
        HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
        DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
    } catch (Throwable t) {
        t.printStackTrace();
        System.exit(1);
    }
}

/**
 * Creates an authorized Credential object.
 * @return an authorized Credential object.
 * @throws IOException
 */
public static Credential authorize() throws IOException {
    // Load client secrets.
   BufferedReader br = new BufferedReader( new FileReader("C:\Users\User\.credentials\calendar-java-quickstart\client_secret.json"));
   GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, br);

    // Build flow and trigger user authorization request.
    GoogleAuthorizationCodeFlow flow =
            new GoogleAuthorizationCodeFlow.Builder(
                    HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
            .setDataStoreFactory(DATA_STORE_FACTORY)
            .setAccessType("offline")
            .build();
    Credential credential = new AuthorizationCodeInstalledApp(
        flow, new LocalServerReceiver()).authorize("user");
    System.out.println(
            "Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
    return credential;
}

/**
 * Build and return an authorized Calendar client service.
 * @return an authorized Calendar client service
 * @throws IOException
 */
public static com.google.api.services.calendar.Calendar
    getCalendarService() throws IOException {
    Credential credential = authorize();
    return new com.google.api.services.calendar.Calendar.Builder(
            HTTP_TRANSPORT, JSON_FACTORY, credential)
            .setApplicationName(APPLICATION_NAME)
            .build();
}


public static void main(String[] args) throws IOException {
    // Build a new authorized API client service.
    // Note: Do not confuse this class with the
    //   com.google.api.services.calendar.model.Calendar class.
    com.google.api.services.calendar.Calendar service =
        getCalendarService();
    String pageToken = null;
    // List the next 10 events from the primary calendar.
    DateTime now = new DateTime(System.currentTimeMillis());
    Events events = service.events().list("primary")
        .setPageToken(pageToken)
        .setMaxResults(10)
        .setTimeMin(now)
        .setOrderBy("startTime")
        .setSingleEvents(true)
        .execute();
    List<Event> items = events.getItems();
    do 
    {
        CalendarList calendarList = service.calendarList().list().setPageToken(pageToken).execute();
        List<CalendarListEntry> items2 = calendarList.getItems();

        for (CalendarListEntry calendarListEntry : items2)
        {
             System.out.println(calendarListEntry.getSummary());
        }//for
          while(pageToken != null)
          pageToken = calendarList.getNextPageToken();
    }//do
    while (pageToken != null);
    System.out.println("------------------------------");
    if (items.size() == 0) 
    {
        System.out.println("No upcoming events found.");
    }//if
    else 
    {
        for (Event event : items) 
        {
            DateTime start = event.getStart().getDateTime();
            if (start == null) 
            {
                start = event.getStart().getDate();
            }//if
            System.out.println(event.getSummary());
        }//for
    }//else
}//main

如您所见,我可以获得所有事件(来自主日历)和可用日历的所有名称。

如何获取 calendarId 以便按日历对事件进行排序?

日历列表returns clandarListResponse

{
  "kind": "calendar#calendarList",
  "etag": etag,
  "nextPageToken": string,
  "nextSyncToken": string,
  "items": [
    calendarList Resource
  ]
}

您已经在循环处理每个 calendarListEntry,但您不应进行摘要,而应仅在本例中请求所需的字段 ID。

每个 calendar#calendarListEntry 都有一个 id 值,它是您要查找的日历 id。

我不是 java 开发人员,但我猜是

for (CalendarListEntry calendarListEntry : items) {
    System.out.println(calendarListEntry.getId());
 }