Printing calendar names of a Google Calendar User
From Wiki
Learn how to access a Google Calendar Account and then print the available list of calendars of the authenticated user. This example is based upon the API Developer's Guide for Java. Google Calendar APIs are used to access the calendar data. Google Calendar APIs are part of the Google Data APIs. Google provides the reader with a conceptual overview of the Data APIs.
Note: The tutorial assumes that the user is familiar with Java programming language and with Eclipse IDE.
Contents |
Minimal Requirements
The minimal list of requirements is based on getting started guide for Google Data APIs page.
- Java JDK (Java Development Kit) 1.5+ - http://java.sun.com/javase/downloads/index.jsp
- Apache Ant version 1.7+ - http://ant.apache.org/
- mail.jar in Sun's JavaMail API 1.4+ - http://java.sun.com/products/javamail/downloads/index.html
- activation.jar in Sun's JavaBeansActivationFramewrok. - http://java.sun.com/products/javabeans/jaf/downloads/index.html
- servlet.jar in Sun's Servlet API version 2.3+. - http://tomcat.apache.org/download-60.cgi
- gData APIs - http://code.google.com/p/gdata-java-client/
The Environment used to run this example
- Operating System: Windows XP
- Java Environment: JDK 1.6.7
- Eclipse SDK "Ganymede" 3.4.1, Build: M20080911-1700
- Apache Ant 1.7
- JavaMail API 1.4
- JavaBeans(TM) Activation Framework Specification 1.1
- Servlet API 2.3
- gData API 1.22.0
Step1: Create a CalendarService.
CalendarService myService = new CalendarService("calendarServiceName");
The name of the calendar service is a user defined string.
Step2: Setup your user credentials
Authentication is needed. Actually Google provides several ways for authentication but here the ClientLogin method is used. For more details check the Google Data APIs Authentication Overview.
UserName and Password must be specified.
myService.setUserCredentials("userName@googlemail.com", "yourPassword");
Step3: Specify the feed URL to query
Notice that URL refers to the calendar URL ( in this example http://www.google.com/calendar/), and we expect feeds (feeds/), and we actually want to list completely (full) all the calendars of the authenticated user ( allcalendars/). Therefore the URL we have to access is:
URL feedUrl = new URL("http://www.google.com/calendar/feeds/default/allcalendars/full");
Step4: Query the feeds
CalendarFeed resultFeed = myService.getFeed(feedUrl, CalendarFeed.class);
CalendarFeed provide a list (java.util.List) of entries (com.google.gdata.data.calendar.CalendarEntry ) result is actually a list (java.util.List) as the result of an SQL query. To print the entry titles of the calendars we have to loop over the entries list.
List<CalendarEntry> calendarEntries = resultFeed.getEntries();
if (calendarEntries !=null){
for (int i = 0; i < calendarEntries.size(); i++) {
CalendarEntry entry = calendarEntries.get(i);
System.out.println("\t" + entry.getTitle().getPlainText());
}
}
The Complete Code
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.data.calendar.CalendarEntry;
import com.google.gdata.data.calendar.CalendarFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
public class CalendarTest {
public static void main(String args[]){
CalendarService myService = new CalendarService("calendarServiceName");
try{
myService.setUserCredentials("userName@googlemail.com", "yourPassword");
URL feedUrl = new URL("http://www.google.com/calendar/feeds/default/allcalendars/full");
CalendarFeed resultFeed = myService.getFeed(feedUrl, CalendarFeed.class);
System.out.println("Your calendars:");
System.out.println();
List<CalendarEntry> calendarEntries = resultFeed.getEntries();
if (calendarEntries != null){
for (int i = 0; i < calendarEntries.size(); i++) {
CalendarEntry entry = calendarEntries.get(i);
System.out.println("\t" + entry.getTitle().getPlainText());
}
}
}
catch(AuthenticationException ae){
ae.printStackTrace();
}
catch(MalformedURLException me){
me.printStackTrace();
}
catch(ServiceException se){
se.printStackTrace();
}
catch(IOException ioe){
ioe.printStackTrace();
}
}
}
Further Reading
For an in depth reading regarding the Java based Google Calendar APIs check the API Developer's Guide for Java.
Besides the Java programming language the APIs are offered also in Python, .NET, PHP, JavaScript.
