Posted by : Munish Gogna Sunday, January 16, 2011

Few days back I came across Spring social API library developed to access social networking sites from within the Java programs and finally Today I got chance to explore it :)

The current milestone includes templates for Twitter, Facebook and LinkedIn. These templates provide an easy way to access each social networking site and make integrating with those sites relatively straight forward. Each of these templates extend Spring's RestTemplate which is a generic template for accessing REST based web services. All hard core fans of Spring's JDBCTemplate and HibernateTemplate will love this.

To use Spring social:
1. Set up developer account with target social networking site.
2. Request an OAuth token from the target social networking site for each new user.
3. Use the right Spring Source template (e.g TwitterTemplate), passing in the developer keys (ApiKey and ApiSecret) as well as OAuth key for the current user.
4. Thats all :)

Once authorized, the application code required to access any given social site is very easy, so easy that even MBA guy can do it!

Check it out:
//Get Twitter template
TwitterTemplate twitter = new TwitterTemplate(apiKey, apiSecret, accessToken, accessTokenSecret);
twitter.updateStatus("Hey, I'm tweeting with #Spring Social! wow");

//Get Facebbook template
FacebookTemplate facebook = new FacebookTemplate(accessToken);
facebook.updateStatus("Hey, I'm posting with #Spring Social! wow");

//Get LinkedIn template
LinkedInTemplate template = new LinkedInTemplate(apiKey, apiSecret, accessToken, accessTokenSecret);
List <linkedinprofile> connections = template.getConnections(); 

The OAuth authorization part is not as clean as I would have liked. SpringSource has created a reference implementation called Greenhouse to demonstrate how to use the API.

Don't know what is OAuth?
So what is up with this OAuth. In just a few words, it is an authentication service. As an application you forward a visitor to the authentication service. The visitor authenticates itself and your application receives the result using a callback. If the result is positive, the visitor has authorized your application to act on the behalf of the visitor. That way you can use services of the provider without knowing who you are dealing with. It is also possible to store the provided authentication for future use (Greenhouse has this featute).

However we can also use some other OAuth libraries to avoid complexities in GreenHouse API and then just use the REST-like APIs of each service by directly manipulating URLs. The most interesting, easy to use and simple I found is Scribe, its a cool API that provides support for Facebook, Google ,Yahoo, LinkedIn, Twitter, Foursquare, Evernote, Vimeo and Yammer.

The latest version is 1.0.9 that should include Facebook API (as per GitHub), as of now this change is not available for 1.0.9 version in maven, I built it manually getting source from github repository.
You can pull scribe from a maven repository, just add this to your pom.xml file.


Now its time to create the OAuthService object
The OAuthService is the one you use to sign your requests. You create it using Scribe’s ServiceBuilder like shown below.

You can use the Facebook 'SpringSocialAPI' app that I created, details are:
Application ID: 175348469171013
Application Secret: c3301704fdd6da30b5b8ab68f9808b09

String apiKey = "c97dc03050b89baf893347f40f177d55";
    String apiSecret = "c3301704fdd6da30b5b8ab68f9808b09";
    OAuthService service = new ServiceBuilder()
The token will be generated and will be available on the mentioned callback url.

You need to get a request token, like this:
Token requestToken = service.getRequestToken();
String your_token = requestToken.getToken();

Now go authorize it! for this make a request to the authorize url (this varies between apis).
For Facebook this is it:

Thanks, see u soon!!!

3 Responses so far.

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Popular Post


enums (1) java (2) JAX-RS (1) JPA (1) mysql (1) request 2 (1) RESTful (1) sphinx (1) tomcat (1) web service (2) ws (2)