简体   繁体   中英

How do I get all the calendar entries for a particular time range using Google Calendar API

I want to view events over specific time range for a specific calendar, but am having trouble using the API, It is a generic API, and it reminds me of using the DOM. The problem is that it seems difficult to work with because much of the information is in generic base classes.

How do I get the events for a calendar using Groovy or Java? Does anybody have an example of passing credentials using curl?

Example code would be appreciated.

If you do not need to alter the calendar, you only need to get the calendars private feed url, and you can use something like this (taken from the http://eu.gr8conf.org/agenda page). It uses the 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

Happy hacking.

This document has examples for most of the common use cases. For example, here's the code for retrieving events for a specific time range

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);

You could make this a bit Groovier, using something like:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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