Google Calendar API Overview, Part II

(previous: part I)

Retrieve Calendars

Google Calendar defines three types of calendars: primary, secondary and imported. Primary is the calendar that is created when the user is signed up for the Google Calendar service. This calendar can not be deleted. Secondary calendars are all the other calendars that the user creates manually. This type of calendar can be created and deleted. The third type – imported calendars, are calendars for which the user is subscribed. These are calendars created by other users.

To retrieve user’s calendars you have to execute authenticated GET http request to particular feed URL. If you want to retrieve all of the user’s calendars you have to request the allcalendars feed URL which is:

http://www.google.com/calendar/feeds/default/allcalendars/full

If you want to retrieve only calendars that user owns you have to refer to owncalendars feed URL which is:

http://www.google.com/calendar/feeds/default/owncalendars/full

You can retrieve the list of calendars by using getFeed method of CalendarService. Here is a snippet how to get all calendars of a user using ClientLogin authenticaiton.

// Create a CalendarService and autheticatate with UserLogin

CalendarService calendarService = new CalendarService(Bianor-Test-0.1);

calendarService.setUserCredentials(“user@gmail.com”, “password”);

URL feedURL = new URL(“http://www.google.com/calendar/feeds/default/allcalendars/full”);

CalendarFeed calendarFeed = calendarService.getFeed(feedURL, CalendarFeed.class);

List<CalendarEntry> entryList = calendarFeed.getEntries();

Operations with Calendars

For all the examples that follow I assume there is a CalendarService created and already authenticated, called calendarService.

1. Creating new calendars

There are several steps to be executed in order to create a new calendar. The first one is to authenticate and obtain CalendarService instance and I assume that it is already obtained. The second one is to create the CalendarEntry instance. The third one is to create the feed URL and to add the instance to CalendarService. Here is a snippet of code that illustrates the steps.

// crate calendar entry

CalendarEntry calendar = new CalendarEntry();

calendar.setTitle(new PlainTextConstruct(“TestCalendar”));

calendar.setSummary(new PlainTextConstruct(“This calendar is created while testing Google calendar API”));

calendar.setTimeZone(new TimeZoneProperty(“Europe/Sofia”));

calendar.setHidden(HiddenProperty.FALSE);

calendar.setColor(new ColorProperty(“#2952A3″));

calendar.setCanEdit(true);

// insert the entry

URL feedURL = new URL(“http://www.google.com/calendar/feeds/default/owncalendars/full”);

calendar = calendarService.insert(feedURL, calendar);

2. Update calendars

You can update properties of existing calendars. You can call any appropriate setXXX() method and then call the update method of the CalendarEntry instance.

calendar.setCanEdit(false);

calendar.update();

3. Deleting calendars

There are two ways to delete calendars. The first one is to delete calendar by calling delete method of corresponding CalenarEntry instance.

calendar.delete();

The second is by calling delete method of CalendarService instance. You have to specify the URL of the object you want to delete:

calendarService.delete(new URL(calendar.getId()));

Operations with Events

The operations for event manipulations follow the same steps as for calendars. To manage events for default calendar of the user testBianor@gmail.com the URL is:

http://www.google.com/calendar/feeds/testBianor@gmail.com/private/full

For all examples with events there is a URL instance EVENTS_URL constructed as shown by the snippet:

URL EVENTS_URL = new URL( “http://www.google.com/calendar/feeds/testBianor@gmail.com/private/full”);

1. Retrieving events

To retrieve events for particular calendar one has to execute a code as the one shown in the snippet bellow:

CalendarEventFeed calendarEventFeed = calendarService.getFeed(EVENTS_URL, CalendarEventFeed.class);

List<CalendarEventEntry> eventsList = calendarEventFeed.getEntries();

With Google Calendar Java API you can also retrieve events by executing day search or full text search.

2. Creating events

To create single occurrence event you may execute the code shown bellow:

CalendarEventEntry eventEntry = new CalendarEventEntry();

eventEntry.setTitle(new PlainTextConstruct(”Test event”));

eventEntry.setContent(new PlainTextConstruct(”Some content here”));

DateTime startTime = DateTime.parseDateTime(”2008-05-22T15:00:00-08:00″);

DateTime endTime = DateTime.parseDateTime(”2008-05-22T15:00:00-08:00″);

When eventTime = new When();

eventTime.setStartTime(startTime);

eventTime.setEndTime(endTime);

eventEntry.addTime(eventTime);

Where where = new Where(”", “”, “Plovdiv”);

eventEntry.addLocation(where);

calendarService.insert(EVENTS_URL, eventEntry);

First step is to create new instance of com.google.gdata.data.calendar.CalendarEventEntry which is the class that wraps events. Then the title and the content are being set. The title may be a plain text, URL, HTML according to the instance used as parameter. The content may also be a plain text, HTML, or event XML or binary data. After that, new instances are created - one for the period of the event and one for the position. The final step is to insert the event with insert method of CalendarService instance.

3. Quick create of events

There is a possibility to create events quickly. The only thing is to mark the event as QuickAdd by calling the setQuickAdd method:

CalendarEventEntry eventEntry = new CalendarEventEntry();

eventEntry.setContent(new PlainTextConstruct(”QuickTestEvent May 25 14:00-14:40″));

eventEntry.setQuickAdd(true);

calendarService.insert(EVENTS_URL, eventEntry);

The example creates new event with a title: QuickTestEvent for 25th, May.

4. Create Google gadget event

With Google Calendar Java API you can also create Google Calendar Gadget events. Calendar Gadget is also known as web content and can contain images, HTML pages or Google Gadget.

To create a simple gadget that shows image on the calendar and refers to Bianor’s web page, first create a new instance of com.google.gdata.data.calendar.WebContent class.

// create the web content

WebContent webContent = new WebContent();

webContent.setTitle(“Test pic”);

webContent.setIcon(“http://www.bianor.com/images/b.gif”);

webContent.setUrl(“http://www.bianor.com/”);

webContent.setHeight(“200″);

webContent.setWidth(“200″);

// add the content to the event

eventEntry.setWebContent(webContent);

Gadget shows little icon on the Google Calendar page which opens a window where Bianor’s web-site is shown:

Note that the example does not show how to create and insert the event itself which is covered in previous section..

5. Update events

In order to update an event just change any property and call update method of the instance

6. Deleting events

You can delete an event by calling delete method of EventEntry instance

eventEntry.delete();

or by calling delete method of the CalendarService instance.

Other Calendar features

Calendar API provides other very useful features such as: managing subscriptions to calendars, share calendars, execute multiple operations with single request, insert specific project properties into events and many more.

Resources

[download] http://code.google.com/p/gdata-java-client/downloads/list

http://code.google.com/apis/calendar/developers_guide_protocol.html – API Developer’s Guide: The Protocol

http://code.google.com/apis/calendar/developers_guide_java.html – API Developer’s Guide: Java

http://code.google.com/apis/calendar/calendar_gadgets.html - Calendar Gadgets Reference Guide

http://code.google.com/apis/accounts/docs/AuthForWebApps.html - Authentication for Web Applications

Sphere: Related Content

Tags:

Post Comment