10/31/2011

Connecting Liferay to LinkedIn

Whenever you think about using liferay in your business you will surely think of it as an "integration platform": One platform to integrate all the information sources that matter to your customers.

In this blog post we will look at how to integrate LinkedIn into your portal. In one of the next posts we will do the same with facebook, but today we will care about the world´s leading business network.

When we talk about integration we mean: "Getting the Data". Liferay always expects to have the users data in its user table and it doesn´t make any sense to rip that core functionality out of liferay. Our goal is to get you connected to linkedIn, so that you have the following possibilities


  • Authenticate with linkedIn
  • Import data from linkedIn

Preparation


Our first step will be to allow your liferay instance to connect to linkedIn.
To do so, you need to create an application for linkedIn. If you already did that, you can skip this step and continue with step 2.

To be able to connect to linkedIn (no matter what you do) you need to create a linkedIn application. 

Enter all necessary data and create your linkedIn application. If you do so, you should now have an API - Key and a secret password. You need those to create the OAuth authentication flow that is needed to authorize your application to access the data of the users.


What business case ?


Something that you should know before even registering your app at linkedIn is what you want to do with the data you get on linkedIn. Your use cases define the way you need to change or configure the liferay implementation. You might



  • Show job offerings in an own portlet
  • Send linkedIn messages from liferay
  • authenticate the User with linkedIn



etc etc etc 


There is a lot you can do. So think about it, and when you are ready, go on to step 3


Accessing LinkedIn


There are many ways to connect to LinkedIn, but I am only going to show one here. There is a pretty good java API available under http://code.google.com/p/linkedin-j/. It seems to be pretty up to date, still under development and (as far as I could see) offers access to all the data LinkedIn provides.

Download the latest version and put all the jars either into the lib dir of your application server or into WEB-INF\lib of the portlet you are developing. Now you should think about how to implement the OAuth flow for linkedIn (facebook is the same ...). The OAuth authentication mechanism basically consists of two steps:

Get the user-specific access token for every user by letting them autorize your app.

Use the access token to get their data from linkedIn.

The first step is something that can be done pretty good in a servlet because it requires some back - and forth communication between the linkedIn and your server. But if you have all the parameters from your portlet at hand, I am sure you can also embed it into a portlet. The code to let the User authenticate your app at linkedIn is the following:


// Create the oauthService

final LinkedInOAuthService oauthService = LinkedInOAuthServiceFactory.getInstance().createLinkedInOAuthService(your-consumerKey, your-consumerSecret);

//Create the request token to get the autorization URL. You need to specify
//a Callback URL. That´s the URL where the auth token is forwarded to
LinkedInRequestToken requestToken = oauthService.getOAuthRequestToken(CALLBACK_URL);
String authUrl = requestToken.getAuthorizationUrl();

//forward to authorization page
resp.sendRedirect(authUrl);

By implementing this, the user should be transported to the LinkedIn page where he has to authorize your app. If he accepts, he will be transported back to the CALLBACK_URL you specified and you will then have access to his access token. To get his access token, implement the following:

// Get the OAuth Verifier from the request
String oauthVerifier = req.getParameter("oauth_verifier");

//create the oauth service
final LinkedInOAuthService oauthService = LinkedInOAuthServiceFactory.getInstance().createLinkedInOAuthService(your-consumerKey, your-consumerSecret);

//Create the request token 
LinkedInRequestToken requestToken = oauthService.getOAuthRequestToken(CALLBACK_URL);
String authUrl = requestToken.getAuthorizationUrl();

//finally: The access token !!!
LinkedInAccessToken accessToken = oauthService.getOAuthAccessToken(requestToken, oauthVerifier);

If you have this access token, you´re done. You don´t need more to access all fields of the user. Persist this access token somewhere in your database and retrieve it every time you need to update your data. Here is an example of how to get the Users first name:

LinkedInConnector connector = new LinkedInConnector();
LinkedInApiClient client = connector.connect(accessToken);

Person profile = client.getProfileForCurrentUser();
System.out.println("First name: "+profile.getFirstName());

That´s all! All you need to do now is to define your business use case and get all the fields you need. Contact me if you need any assistance.


If you like this tutorial it would be very nice, if you could click on some of the google ads you see on the right side. It helps me run this block and motivates me ;)

If you have any questions, feel free to leave a comment.