views:

53

answers:

2

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.

+1  A: 

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.

sbglasius
+1  A: 

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("[email protected]", "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("[email protected]", "mypassword");

// Send the request and receive the response:
def resultFeed = myService.query(myQuery, Feed);
Don
I will try this out soon, but doesn't this just get the calendar's. I need the actual calendar entries?
BeWarned
No, it returns the entries, notice that the return type (in the Java code), is `CalendarEventFeed`, i.e. it returns calendar events
Don