Thursday, September 22, 2011

Spring Social Meets Google APIs

For the past several weeks I have been working on the Spring Social Google project. The goal of Spring Social is to act as an abstraction layer between your application code and various social APIs, and removes the need for you to deal with authentication and HTTP-Java mapping, and now you can use it with a growing number of Google APIs, starting with the Contacts API and Google+ API.

Why do I need this? Google already provides Java libraries for its APIs.

Indeed you can use the Java libraries from Google, but there are cases where you may benefit from using Spring Social instead. For once, Spring Social comes with an authentication mechanism that removes the need for you to write Servlet code for the stages of the OAuth2 process. You can read about it here. Another goodie Spring Social provides is connection management and persistence, so you don't have to deal with session management and and writing your own database schema to store the users' access tokens. You can read about it here. You can see how to set up both mechanisms in the example application, specifically in SocialConfig.javaWebMvcConfig.java and spring-config.xml .

Spring Social also takes a different approach than Google's Java libraries in that Google's libraries provide a Java API that mirrors the underlying REST API (be it Atom, Portable Contacts or other), while Spring Social aims to provide you with data structure and operations you are likely to use.
Let's use both tools to write a program that fetches the user's contacts and prints their names and primary e-mail addresses.

Here is how it's done with Google Contact API Java library:

ContactsService service = getContactsService();
URL feedUrl = new URL("https://www.google.com/m8/feeds/contacts/default/full");
ContactFeed resultFeed = service.getFeed(feedUrl, ContactFeed.class);
for (ContactEntry entry : resultFeed.getEntries()) {
   String fullName = entry.getName().getFullName().getValue();
   String email = null;
   for(Email e : entry.getEmailAddresses() {
      if(email.getPrimary) {
         email = e;
         break;
      }
   }
   System.out.println(fullName + " " + email);
}

And here is the equivalent Spring Social Google code:

GoogleOperations google = getGoogleOperations();
List<Contact> contacts = google.contactOperations().getContactList();
for(Contact contact : contacts) {
   String fullName = contact.getFullName();
   String email = contact.getPrimaryEmail();
   System.out.println(fullName + " " + email);
}

It's up to you to decide if you prefer the simplicity of Spring Social or the flexibility of the Google API Java libraries.

Is the project stable? What is its roadmap?

Spring Social Google is still at an early stage, so there are no builds and the API may change. Aside of the currently implemented Contacts and Google+ APIs, the project is expected to include more and more of Google APIs over time. You are welcome to download the sources and play with the example application, which is also deployed on Cloud Foundry

Enjoy and send feedback!

2 comments:

  1. Hi, any update on this? How about inserting "moments" to Google+, is that planned as well ?

    ReplyDelete
  2. Love to read it,Waiting For More new Update and I Already Read your Recent Post its Great Thanks. myigfollowers.com

    ReplyDelete