BT
x Your opinion matters! Please fill in the InfoQ Survey about your reading habits!

First Spring Social Milestone to Integrate with Twitter, Facebook, LinkedIn and Tripit

by Tim Cull on Nov 08, 2010 |

Last week SpringSource released a first milestone for Spring Social, a Spring-based template for accessing social networking sites from within Java programs.

This first milestone includes templates for Twitter, Facebook, LinkedIn and Tripit. Rather than exposing generic, URL-based APIs, the Spring Social APIs are specifically designed for each social networking site and make integrating with those sites relatively straight forward. Each template is, in turn, built on top of Spring's RestTemplate, which is a generic template for accessing any REST-based web service.

To use Spring Social, developers:

  1. Manually set up a one-time developer account with the target social networking site.
  2. Programmatically request an OAuth token from the target social networking site for each new user.
  3. Instantiate the right Spring Source template (ie. TwitterTemplate), passing in their developer keys as well as the OAuth key for the current user.
  4. Call whatever method they like on the Spring Source template object, much like calling methods on a JdbcTemplate.

 

Once authorized, the application code required to manipulate a given social networking site is nearly trivial, as in the below example to get a user's LinkedIn connections:

LinkedInTemplate template = new LinkedInTemplate(developerApiKey, developerSecret, oauthAccessTokenValue, oauthAccessTokenSecret);
List <linkedinprofile> connections = template.getConnections();

Of course, the OAuth authorization portion is the tricky part. SpringSource has created a reference implementation called Greenhouse to demonstrate how to use the API. Greenhouse has some sample code that demonstrates requesting OAuth tokens and connecting to each provider, but an excellent Spring Social tutorial already written by Jettro Coenradie might be easier to follow.

Future milestones for Spring Social plan to include better documentation and more integrated management of OAuth services to store authorizations. At the moment, developers have to handle storing authorization keys somewhere and passing authorization keys to the Spring Social template. But the next Spring Social milestone will include code to make this process easier. SpringSource plans to base its ultimate OAuth solution on the code currently in Greenhouse.

Alternatives to Spring Social include using other, site-specific libraries. Twitter has the richest ecosystem of libraries like Twitter4J and others. Site-specific libraries for other sites are a little harder to come by, like the fork of Facebook's formerly official, now semi-abandoned Java API or RestFB.

Developers can also use OAuth helper libraries to do some heavy lifting and then just use the REST-like APIs of each service by directly manipulating URLs. Some popular OAuth libraries include Signpost, OAuth for Spring Security, Scribe, and others.

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

What does this replace? by Daniele Dellafiore

It's easy to understand that this is an integrated and spring-friendly alternatives to different social network specialized libraries but what about general purpose OAuth helper libraries like OAuth for spring and such?
For exampl, I am starting to develop an app that uses the new Salesforce REST API that allow OAuth2 authentication. What's a suggested approach? Try to implement a SalesforceTemplate like the ones in Spring Social using the low level API or maybe use OAuth for Spring?

Craig Walls by Keith Donald

Craig Walls, author of Spring in Action, is the lead of the Spring Social project, and has written a detailed blog post on the capabilities of the first Spring Social milestone, which includes a look to the future as well as step-by-step instructions on getting the Greenhouse reference application running locally for exploring its architecture. You can read Craig's article at the SpringSource Team Blog.

A note about Greenhouse: while it does serve as a sample application, it is also hosted in production at greenhouse.springsource.org, and was used to power the recent SpringOne2gx conference in Chicago, IL, allowing members to follow the event in real-time from their mobile phones. I point this out because you'll find production quality application code you can use not only as a learning tool, but code you can look to take advantage of in your own applications.

Keith

Re: What does this replace? by Keith Donald

Spring Social does not replace anything--it's a higher-level project to help you build "social-ready" web apps that integrate various service providers such as Facebook and Twitter. It depends on a general purpose OAuth helper library to do this. As discussed in the comments on Craig's blog, Spring Social can work with two different OAuth libraries at present:
#1. Spring Security OAuth, a subproject of Spring Security lead by Ryan Heaton that supports both OAuth 1 and 2 comprehensively.
#2. Scribe, a OAuth 1.0 client library lead by Pablo Fernandez with a nice API.

For your specific case, I would consider using the OAuth 2 client support in the Spring Security OAuth project. It's first milestone release under the org.springframework.security umbrella is now available as well. Yes, I would recommend implementing a SalesforceOperations API similar to what you see in Spring Social for Twitter, Facebook, LinkedIn, and TripIt. This allows you to hide the OAuth and REST details, and gives callers a clean domain-oriented API to use.

I would also recommend reviewing the "Service Provider" framework currently implemented in the Greenhouse project that provides an extensible model for registering new providers and establishing connections with them. As both Craig and Tim mentioned, this framework is being factored out from Greenhouse into Spring Social in a subsequent milestone. It makes the process of integrating a new ServiceProvider such as Salesforce into your application something like this:
1. Create an API interface/implementation for the new ServiceProvider e.g. SalesforceOperations/SalesforceTemplate.
2. Create a implementation of the ServiceProvider<S> interface e.g. SalesforceServiceProvider<SalesforceOperations>
3. Register the ServiceProvider<S> implementation with the system.
Once registered, users of your application can then establish connections to the provider. Once a connection is established, the application can then the Provider's Operations interface to invoke the API on behalf of the user.

Keith

Re: What does this replace? by Craig Walls

As Keith mentioned, Spring Social depends on an OAuth library to handle the task of signing requests with OAuth credentials. In milestone 1, Spring Social expects that you will have registered your application with the OAuth provider to receive your API key and secret and will have already gone through the "OAuth dance" to receive an access token for your user.

However, a major focus of milestone 2 will be to bring the service provider framework in Greenhouse to Spring Social so that there is a more complete story of how OAuth details can be managed. To be a part of the Spring Social discussion, I invite you to visit the Spring Social forum at forum.springsource.org/forumdisplay.php?f=82. We want this project to be community-driven, so your input and involvement is greatly appreciated.

As the OAuth 2 is still quite new, there aren't many options available for working with OAuth 2. But I encourage you to check out Ryan Heaton's Spring Security for OAuth (static.springsource.org/spring-security/oauth/s...), as it has client support for OAuth 2 in its first milestone release. Also, be sure to visit the Spring Security for OAuth forum at forum.springsource.org/forumdisplay.php?f=79 for discussion on that topic.

Re: What does this replace? by Daniele Dellafiore

Thanks for your answers Craig and Keith.

I added spring social and oauth to my project where just yesterday I started to integrate with the brand new REST OAuth2 API for Salesforce. I'll move this discussion on the forum so maybe I will emerge with a SalesforceTemplate implementation instead of a custom one.

Sample Code on Spring Social Accessing Facebook by Paul Sydney Orozco

I have made a sample code on how to use Spring Social in Accessing Facebook. Just wanna share it and hope it would help others too.. Accessing Facebook Using Spring Social .

Re: Sample Code on Spring Social Accessing Facebook by suresh m

Paul,
The links is not working and i am getting a 404. Do you have an updated link?

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

7 Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT