Integrating LinkedIn with grails application

19 / Mar / 2012 by Vishal Sahu 1 comments

In my recent grails project, i needed to integrate LinkedIn with the application using LinkedIn API. I searched a lot about it and find the Java wrapper of linkedIn to use it with my grails application and thought it worth sharing.
LinkedIn uses OAuth2.0 protocol for authorization when our application tries to access the data.


LinkedIn provides 2 ways to access its data

  1. REST API
  2. JavaScript API

I used the REST API to make calls for accessing data.

The steps involved in using LinkedIn API in our grails application are as:-


#1. Register Application :

Register an application on LinkedIn here and get an API key.
This process will generate application and provide API Key & Secret Key for getting access token for API calls.


#2. Get Access Token :

With the help of API key and API secret generated in the above step, we need to get access token for calls. There are various wrapper libraries available to get the access token.


The Java wrapper which i used can be downloaded from here.


Steps involved in getting access token are same for any social media using Oauth 2.0 protocol.
#. A request token is sent to the server to get a URL for the user to give back a verification code.
#. The verification code and the request token are used in another request to get an access token.
Once we have that access token we can then use it to get data from the LinkedIn.


Code to get verification_code is as :

[java]

def registerOnLinkedIn = {

String apiKey = API_KEY obtained from registered LinkedIn Application
String apiSecret = API_SECRET obtained from registered LinkedIn Application
String callbackurl = http://www.example.com/myapp/linkedInCallback
// callback URL to get the access token

LinkedInOAuthService oauthService = LinkedInOAuthServiceFactory.getInstance()
.createLinkedInOAuthService(apiKey, apiSecret)
LinkedInRequestToken linkedInRequestToken= oauthService.getOAuthRequestToken(callbackurl);
String authUrl = linkedInRequestToken.getAuthorizationUrl()
session[‘REQUEST_TOKEN’] = linkedInRequestToken
// set linkedRequest token in session, as we will be requiring it to get access token
redirect(url: authUrl)

}

[/java]

This will redirect user to linkedIn server to grant permissions for this application and provides an verification code to obtain access token

As the user grants permission, the linkedin will redirect it back to the application with verification code, which is used to retrieve access_token.


Code to handle callback, where linkedIn sends verification code.


[java]

def linkedInCallBack = {
LinkedInOAuthService oauthService = LinkedInOAuthServiceFactory.getInstance()
.createLinkedInOAuthService(apiKey, apiSecret)
LinkedInRequestToken requestToken = (LinkedInRequestToken) session[‘REQUEST_TOKEN’]
List<String> ouathVerifier = params.list("oauth_verifier")
LinkedInAccessToken linkedInAccessToken = oauthService.getOAuthAccessToken(requestToken, ouathVerifier[0])
String accessToken = linkedInAccessToken.token
// required access token for API calls.
}

[/java]


This will generate access token for API calls.


This worked for me.
Hope it helps.

Useful Links:

http://code.google.com/p/linkedin-j/


Cheers!!!
Vishal Sahu
vishal@intelligrape.com

FOUND THIS USEFUL? SHARE IT

comments (1 “Integrating LinkedIn with grails application”)

  1. Lasse

    Hi,

    Thanks for the tutorial. One thing I wonder about is that as you describe (and I am also getting) is the confirmation screen that the user is prompted. If I go to cleancoders.com I don’t have to do that confirmation. Maybe I did it the first time and cleancoders stores it from then on?

    Best regards /Lasse

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *