简体   繁体   English

带有Java的Google Calendar API-仅获取默认日历的事件,忽略假期

[英]Google Calendar API w/ Java - Get events only for the default calendar, ignoring the holidays

What I am trying to do is get a list of all the events in the default calendar only? 我想做的是仅获取默认日历中所有事件的列表? I don't want any other calendars' events included, ie, holidays. 我不希望包含其他日历活动,例如假期。

I am using the ".../feeds/username/default/full" and doing a query for all events from 1 month ago to forever. 我正在使用“ ... / feeds /用户名/ default / full”并查询从1个月前到永远的所有事件。 I have two events that I have created but I get a feed with 25 entries. 我创建了两个事件,但获得了包含25个条目的供稿。 My two events first and then 23 holidays. 我的两个事件是第一个,然后是23个假期。 I really don't them to be included at all but if them MUST be there, how do I detect them. 我真的根本不将它们包括在内,但是如果它们必须存在,我如何检测到它们。 The only thing I can find is the eTag for my two events is different from the holiday ones. 我唯一能找到的是我的两个活动的eTag与假期的eTag不同。 But I can't just say get all the events from the first Etag I encounter because the default calendar may not have any events so the first one would be holidays. 但是我不能只说从遇到的第一个Etag中获取所有事件,因为默认日历可能没有任何事件,因此第一个将是假期。

Any help would be appreciated. 任何帮助,将不胜感激。

This can be done with a few lines of code using the latest version of the Google Calendar API. 使用最新版本的Google Calendar API,只需几行代码即可完成此操作。 All you need to do is pull the events for the primary calendar, as documented here: 您需要做的就是提取主日历的事件,如此处所述:

http://code.google.com/apis/calendar/v3/using.html#retrieving_events http://code.google.com/apis/calendar/v3/using.html#retrieving_events

The code you need is: 您需要的代码是:

Events events = service.events().list("primary").execute();

while (true) {
  for (Event event : events.getItems()) {
    System.out.println(event.getSummary());
  }
  String pageToken = events.getNextPageToken();
  if (pageToken != null && !pageToken.isEmpty()) {
    events = service.events().list("primary").setPageToken(pageToken).execute();
  } else {
    break;
  }
}

I wonder what version of the Calendar API you are using? 我想知道您正在使用哪个版本的Calendar API? In v3 I am able to all my events without getting the holidays. 在v3中,我无需休假就能参加所有活动。 I added some more code examples, since the Google documentation is incorrect for connecting to the Calendar V3 API though Oauth2 for Java. 我添加了更多代码示例,因为Google文档通过Java Oauth2连接到Calendar V3 API不正确。

Use the code listed in the other post to loop though the Calendar events after the Oauth is setup and the Calendar is initialized. 使用其他文章中列出的代码来循环遍历设置Oauth和初始化日历后的Calendar事件。 You probably can, but do not need to declare the events API. 您可能可以,但不需要声明事件API。 I just call it from the Calendar service(3). 我只是从Calendar service(3)调用它。 Note the Calendar ID I have listed is "Primary." 请注意,我列出的日历ID是“主要”。

Calendar service3 = new Calendar(transport, jsonFactory, accessProtectedResource);

com.google.api.services.calendar.model.Calendar calendar = service3.calendars().get("primary").execute();
com.google.api.services.calendar.model.Events events = service3.events().list("primary").execute(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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