{"id":5125,"date":"2012-03-19T00:20:53","date_gmt":"2012-03-18T18:50:53","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=5125"},"modified":"2016-12-19T15:16:40","modified_gmt":"2016-12-19T09:46:40","slug":"integrating-linkedin-with-grails-application","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/integrating-linkedin-with-grails-application\/","title":{"rendered":"Integrating LinkedIn with grails application"},"content":{"rendered":"<p>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.<br \/>\nLinkedIn uses OAuth2.0 protocol for authorization when our application tries to access the data.<\/p>\n<pre><\/pre>\n<p>LinkedIn provides 2 ways to access its data<\/p>\n<ol>\n<li><a href=\"https:\/\/developer.linkedin.com\/rest\">REST API<\/a><\/li>\n<li>JavaScript API<\/li>\n<\/ol>\n<p>I used the REST API to make calls for accessing data.<\/p>\n<p>The steps involved in using LinkedIn API in our grails application are as:-<\/p>\n<pre><\/pre>\n<p>#1.<strong> Register Application :<\/strong><\/p>\n<p>Register an application on LinkedIn <a href=\"https:\/\/www.linkedin.com\/secure\/developer\">here<\/a> and get an API key.<br \/>\nThis process will generate application and provide <strong>API Key<\/strong> &amp; <strong>Secret Key<\/strong> for getting access token for API calls.<\/p>\n<pre><\/pre>\n<p>#2. <strong>Get Access Token : <\/strong><\/p>\n<p>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 <a href=\"https:\/\/developer.linkedin.com\/documents\/libraries-and-tools\">libraries<\/a> available to get the access token.<\/p>\n<pre><\/pre>\n<p>The Java wrapper which i used can be downloaded from <a href=\"http:\/\/code.google.com\/p\/linkedin-j\/\">here<\/a>.<\/p>\n<pre><\/pre>\n<p>Steps involved in getting access token are same for any social media using Oauth 2.0 protocol.<br \/>\n<strong>#. <\/strong> A request token is sent to the server to get a URL for the user to give back a verification code.<br \/>\n<strong>#.<\/strong> The verification code and the request token are used in another request to get an access token.<br \/>\nOnce we have that access token we can then use it to get data from the LinkedIn.<\/p>\n<pre><\/pre>\n<p>Code to get verification_code is as :<\/p>\n<p>[java]<\/p>\n<p>def registerOnLinkedIn = {<\/p>\n<p>  String apiKey = API_KEY obtained from registered LinkedIn Application<br \/>\n String apiSecret = API_SECRET obtained from registered LinkedIn Application<br \/>\n String callbackurl = http:\/\/www.example.com\/myapp\/linkedInCallback<br \/>\n                              \/\/ callback URL to get the access token<\/p>\n<p>  LinkedInOAuthService oauthService = LinkedInOAuthServiceFactory.getInstance()<br \/>\n                                         .createLinkedInOAuthService(apiKey, apiSecret)<br \/>\n  LinkedInRequestToken  linkedInRequestToken=  oauthService.getOAuthRequestToken(callbackurl);<br \/>\n  String  authUrl = linkedInRequestToken.getAuthorizationUrl()<br \/>\n  session[&#8216;REQUEST_TOKEN&#8217;] = linkedInRequestToken<br \/>\n  \/\/ set linkedRequest token in session, as we will be requiring it to get access token<br \/>\n  redirect(url: authUrl)<\/p>\n<p> }       <\/p>\n<p>[\/java]<\/p>\n<p>This will redirect user to linkedIn server to grant permissions for this application and provides an verification code to obtain access token<\/p>\n<p>As the user grants permission, the linkedin will redirect it back to the application with verification code, which is used to retrieve access_token.<\/p>\n<pre><\/pre>\n<p>Code to handle callback, where linkedIn sends verification code.<\/p>\n<pre><\/pre>\n<p>[java]<\/p>\n<p>def linkedInCallBack = {<br \/>\n        LinkedInOAuthService oauthService = LinkedInOAuthServiceFactory.getInstance()<br \/>\n                                                 .createLinkedInOAuthService(apiKey, apiSecret)<br \/>\n        LinkedInRequestToken requestToken = (LinkedInRequestToken) session[&#8216;REQUEST_TOKEN&#8217;]<br \/>\n        List&amp;amp;lt;String&amp;amp;gt; ouathVerifier = params.list(&amp;amp;quot;oauth_verifier&amp;amp;quot;)<br \/>\n        LinkedInAccessToken linkedInAccessToken = oauthService.getOAuthAccessToken(requestToken, ouathVerifier[0])<br \/>\n        String accessToken = linkedInAccessToken.token<br \/>\n                                         \/\/ required access token for API calls.<br \/>\n   }<\/p>\n<p>[\/java]<\/p>\n<pre><\/pre>\n<p>This will generate access token for API calls.<\/p>\n<pre><\/pre>\n<p>This worked for me.<br \/>\nHope it helps.<\/p>\n<p>Useful Links:<\/p>\n<p><a href=\"http:\/\/code.google.com\/p\/linkedin-j\/\">http:\/\/code.google.com\/p\/linkedin-j\/<\/a><\/p>\n<pre><\/pre>\n<p>Cheers!!!<br \/>\nVishal Sahu<br \/>\nvishal@intelligrape.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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. [&hellip;]<\/p>\n","protected":false},"author":19,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[7],"tags":[55,769,771,770,702,706,772,700],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/5125"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=5125"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/5125\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=5125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=5125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=5125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}