Loading...

February 1, 2009

Get the edit link for a Google Calendar with the Google Data Java Client API

The Google Data Java Client library contains samples on how to add or change events for the private calendar of an user. But what if we want to add or change events to a new calendar we created ourselves? I found the information in this article. The author shows an example with the REST API. Here we will see an example with Java code:

final CalendarService calendarService = new CalendarService("mrhaki.com-blogsamples-1.0");
calendarService.setUserCredentials("user@gmail.com", "password");
final URL feedUrl = new URL("http://www.google.com/calendar/feeds/default/owncalendars/full");
final CalendarFeed calendarFeed = service.getFeed(feedUrl, CalendarFeed.class);
for (CalendarEntry entry : calendarFeed.getEntries()) {
  System.out.println("edit link: [" + entry.getLink(Link.Rel.ALTERNATE, Link.Type.ATOM).getHref() + "]");
}

We get the link to invoke the getLink() method with the correct parameters. We can use this link to create an event for the specific calendar:

final URL postURL = new URL(entry.getLink(Link.Rel.ALTERNATE, Link.Type.ATOM).getHref());
final CalendarEventEntry entry = new CalendarEventEntry();
entry.setTitle("Entry for my new calendar");
entry.setContent("Sample content.");
final When event = new When();
event.setStartTime(DateTime.now());
entry.addTime(event);
service.insert(postURL, entry);