【问题标题】:How do I get all the calendar entries for a particular time range using Google Calendar API如何使用 Google Calendar API 获取特定时间范围内的所有日历条目
【发布时间】:2011-03-02 20:30:19
【问题描述】:

我想查看特定日历的特定时间范围内的事件,但在使用 API 时遇到问题,它是一个通用 API,它让我想起了使用 DOM。问题是它似乎很难使用,因为大部分信息都在通用基类中。

如何使用 Groovy 或 Java 获取日历的事件? 有人有使用 curl 传递凭据的示例吗?

示例代码将不胜感激。

【问题讨论】:

    标签: java groovy gdata-api google-calendar-api


    【解决方案1】:

    如果您不需要更改日历,您只需要获取日历的私有提要 url,您可以使用类似这样的内容(取自 http://eu.gr8conf.org/agenda 页面)。它使用ICal4J library

    def url = "http://www.google.com/calendar/ical/_SOME_URL_/basic.ics".toURL()
    def cal = Calendars.load(url)
    
    def result = cal.components.sort { it.startDate.date }.collect {
      def e = new Expando()
      e.startDate = it.startDate.date
      e.endDate = it.endDate.date
      e.title = it.summary.value
      if (it.location) {
        e.presentation = Presentation.findByName(it.location.value, [fetch:"join"])
      }
    
      e.toString = {
        "$startDate: $title"
      }
    
      return e
    }
    
    result
    

    黑客愉快。

    【讨论】:

      【解决方案2】:

      This document 提供了大多数常见用例的示例。例如,这里是检索特定时间范围内事件的代码

      URL feedUrl = new URL("http://www.google.com/calendar/feeds/default/private/full");
      
      CalendarQuery myQuery = new CalendarQuery(feedUrl);
      myQuery.setMinimumStartTime(DateTime.parseDateTime("2006-03-16T00:00:00"));
      myQuery.setMaximumStartTime(DateTime.parseDateTime("2006-03-24T23:59:59"));
      
      CalendarService myService = new CalendarService("exampleCo-exampleApp-1");
      myService.setUserCredentials("jo@gmail.com", "mypassword");
      
      // Send the request and receive the response:
      CalendarEventFeed resultFeed = myService.query(myQuery, Feed.class);
      

      您可以使用以下方式将其变得更 Groovier:

      def myQuery = new CalendarQuery("http://www.google.com/calendar/feeds/default/private/full".toURL()).with {
        minimumStartTime = DateTime.parseDateTime("2006-03-16T00:00:00");
        maximumStartTime = DateTime.parseDateTime("2006-03-24T23:59:59");
        it
      }
      
      def myService = new CalendarService("exampleCo-exampleApp-1");
      myService.setUserCredentials("jo@gmail.com", "mypassword");
      
      // Send the request and receive the response:
      def resultFeed = myService.query(myQuery, Feed);
      

      【讨论】:

      • 我很快就会试试这个,但这不只是得到日历的。我需要实际的日历条目吗?
      • 不,它返回条目,注意返回类型(在Java代码中)是CalendarEventFeed,即它返回日历事件
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-25
      • 1970-01-01
      • 1970-01-01
      • 2020-10-09
      • 1970-01-01
      • 2020-12-01
      相关资源
      最近更新 更多