LInkedIn API « Intelligrape Groovy & Grails Blogs

Posts Tagged ‘ LInkedIn API ’

Reading comments of LinkedIn wall post

Posted by on September 26th, 2012

Hi,


In one of my grails project, i needed to show the comments on any wall post of linkedin through API. I used the Java wrapper to connect any linkedIn account with the grails application which can be seen here. But somehow this library was not working when we need to fetch comments from any wall post and display them in our UI.


I searched a lot about it but couldn’t find anything appropriate, then i decided to use the linkedIn API directly for retrieving the data. To make GET calls , i used the Scribe java library which can be downloaded from here.


To make API calls on linkedIn, we need to have an authenticated account’s access_token and access_secret, which can be obtained by connecting a linkedIn account with the application as mentioned in this post.


Code to fetch comments on LinkedIn Wall post :-

String consumerKey = CONSUMER_KEY // key obtained by linkedIn app
String consumerSecret = CONSUMER_SECRET // secret obtained from linkedIn app
String accessToken = 'assess_token'
String accessSecret = 'access_secret'
String postId = POST_ID // id of the wall post

 OAuthService service = new ServiceBuilder()
                .provider(LinkedInApi.class)
                .apiKey(consumerKey)
                .apiSecret(consumerSecret)
                .debug()
                .build();
 String url = "http://api.linkedin.com/v1/people/~/network/updates/key=${postId}/update-comments?format=json";
 
       OAuthRequest request = new OAuthRequest(Verb.GET, url);
        org.scribe.model.Token accessToken = new org.scribe.model.Token(accessToken,accessSecret)
        service.signRequest(accessToken, request);
        Response response = request.send();
        String jsonResponse = response.getBody()
         def updates = JSON.parse(jsonResponse) // contains comments data in JSON format

          updates.values.each {def commentData ->
                  println "Comment : ${commentData.comment}"
                  println "Creator: ${commentData.person.firstName}"        
            }

This code will fetch the comments from any linkedin wall post. It worked in my case.


Hope it helps.


Cheers..!!!
Vishal Sahu
vishal@intelligrape.com
www.linkedin.com/in/vishalsahu

Posted in Grails, Groovy

Integrating LinkedIn Groups in grails application.

Posted by on August 24th, 2012

Hi,
In one of my recent Grails application, i needed to integrate LinkedIn groups with the application.
In my previous post, we discussed about Integrating LinkedIn API in any grails application. To add a LinkedIn Group, we first need to authorize the LinkedIn account which has either created that group or is the member/admin of the group with the rights to publish new post in that group.


After obtaining linkedIn_token and linkedIn_secret as mentioned in this blog, we need to make another call to retrieve all the Linked Groups for that account.


Code to retrieve all the LinkedIn Groups is as :-

         String apiKey = API_KEY obtained from registered LinkedIn Application
         String apiSecret = API_SECRET obtained from registered LinkedIn Application

        Map linkedInGroupMap = [:]
     
        LinkedInAccessToken accessToken = new LinkedInAccessToken(linkedIn_token, linkedIn_secret)
        LinkedInApiClientFactory factory = LinkedInApiClientFactory.newInstance(apiKey, apiSecret)
        LinkedInApiClient linkedInClient = factory.createLinkedInApiClient(accessToken)
        GroupMemberships groupMemberships = linkedInClient.getGroupMemberships()
        
       groupMemberships.groupMembershipList.each {GroupMembership groupMembership ->
            linkedInGroupMap[groupMembership.group.id] = groupMembership.group.name
       }

So, the resulting map will contains the key, value pairs of linkedIn groups ID and Name.


Now we can store these Group IDs for making API calls to :-

#. Retrieve group’s profile details.
#. Read, Create, Like, Comment group posts


Example API call to retieve group’s discussion wall posts:


http://api.linkedin.com/v1/groups/${linkedInGroupId}/posts?format=json&count=30&start=30

This will retrieve latest 30 group discussions in JSON format.


This worked for me. Hope this helps…:)


Useful Links:

https://developer.linkedin.com/documents/groups-api
https://developer.linkedin.com/documents/groups-fields
https://developer.linkedin.com/rest



Cheers..!!!
Vishal Sahu
vishal@intelligrape.com
http://www.intelligrape.com

Posted in Grails, Groovy

Integrating LinkedIn with grails application

Posted by on March 19th, 2012

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 :


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)

 }       

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.



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.               
   }



This will generate access token for API calls.



This worked for me.
Hope it helps.

Useful Links:
https://developer.linkedin.com/documents/quick-start-guide
http://code.google.com/p/linkedin-j/


Cheers!!!
Vishal Sahu
vishal@intelligrape.com
http://www.intelligrape.com

Posted in Grails, Groovy