Wednesday, January 4, 2012

Connecting Liferay to LinkedIn

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:


  1. // Create the oauthService  
  2.   
  3. final LinkedInOAuthService oauthService = LinkedInOAuthServiceFactory.getInstance().createLinkedInOAuthService(your-consumerKey, your-consumerSecret);  
  4.   
  5. //Create the request token to get the autorization URL. You need to specify  
  6. //a Callback URL. That´s the URL where the auth token is forwarded to  
  7. LinkedInRequestToken requestToken = oauthService.getOAuthRequestToken(CALLBACK_URL);  
  8. String authUrl = requestToken.getAuthorizationUrl();  
  9.   
  10. //forward to authorization page  
  11. 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:

  1. // Get the OAuth Verifier from the request  
  2. String oauthVerifier = req.getParameter("oauth_verifier");  
  3.   
  4. //create the oauth service  
  5. final LinkedInOAuthService oauthService = LinkedInOAuthServiceFactory.getInstance().createLinkedInOAuthService(your-consumerKey, your-consumerSecret);  
  6.   
  7. //Create the request token   
  8. LinkedInRequestToken requestToken = oauthService.getOAuthRequestToken(CALLBACK_URL);  
  9. String authUrl = requestToken.getAuthorizationUrl();  
  10.   
  11. //finally: The access token !!!  
  12. 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:

  1. LinkedInConnector connector = new LinkedInConnector();  
  2. LinkedInApiClient client = connector.connect(accessToken);  
  3.   
  4. Person profile = client.getProfileForCurrentUser();  
  5. 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.

1 comment:

  1. Thanks for sharing a nice post, If you want to use LinkedIn Messenger in your Gmail without leaving the Gmail. Then you can try our free Chrome Extension

    ReplyDelete