javagroovylinkedin-apilinkedin-j

Cannot fetch LinkedIn shared connections


I am currently working on a Grails application (using Groovy which is similar to Java), where a user can view the profile of other users. On user's profile page, I need to show the LinkedIn connections shared between that user and the viewer's LinkedIn profiles. For LinkedIn integration, I am currently using linkedin-j.jar

Using all that I could guess from the API documentation and Google search, I wrote the following code, which was unsuccessful to fetch shared connections.

Any help will be appreciated.

LinkedInAccessToken targetUserLiAccessToken = new LinkedInAccessToken(targetUserOauthToken, targetUserOauthSecret)
LinkedInApiClient targetUserLiApiClient= linkedInApiClientFactory.createLinkedInApiClient(targetUserLiAccessToken)
Person targetUserLiProrfile=targetUserLiApiClient.getProfileForCurrentUser([ProfileField.ID] as Set)

LinkedInAccessToken currentUserLiAccessToken = new LinkedInAccessToken(currUserOauthToken, currUserOauthSecret)
LinkedInApiClient currentUserLiApiClient= linkedInApiClientFactory.createLinkedInApiClient(currentUserLiAccessToken)
Person resultProfile =  currentUserLiApiClient.getProfileById(targetUserProfile.id, [ProfileField.ID, ProfileField.RELATION_TO_VIEWER] as Set)

List<Person> commonConnections= resultProfile.relationToViewer.relatedConnections.personList

(Here the current User is viewer and target User is the one whose profile is being viewed.)

Upon running this code, I am getting the following results:

resultProfile.relationToViewer.relatedConnections: NULL

resultProfile.relationToViewer.distance: 2

But this is not as expected, the LinkedIn Profiles of both the users have one shared Connection


Solution

  • I was able to fix the issue.

    The points where I was missing are:

    1. The LinkedIn Application whose API Key and Secret I was using to retrieve data was configured to ask only BASIC_PROFILE permission (This was because LinkedIN recently introduced some changes in their API Permissions). NETWORK permissions were also required to access connections info.

    2. In code, I should have used ProfileField.RELATION_TO_VIEWER_RELATED_CONNECTIONS instead of ProfileField.RELATION_TO_VIEWER

    Following is the final piece of code that worked for me:

    LinkedInAccessToken targetUserLiAccessToken = new LinkedInAccessToken(targetUserOauthToken, targetUserOauthSecret)
    LinkedInApiClient targetUserLiApiClient= linkedInApiClientFactory.createLinkedInApiClient(targetUserLiAccessToken)
    Person targetUserLiProrfile=targetUserLiApiClient.getProfileForCurrentUser([ProfileField.ID] as Set)
    
    LinkedInAccessToken currentUserLiAccessToken = new     LinkedInAccessToken(currUserOauthToken, currUserOauthSecret)
    LinkedInApiClient currentUserLiApiClient= linkedInApiClientFactory.createLinkedInApiClient(currentUserLiAccessToken)
    Person resultProfile =  currentUserLiApiClient.getProfileById(targetUserProfile.id, [ProfileField.ID, ProfileField.RELATION_TO_VIEWER_RELATED_CONNECTIONS] as Set)
    
    List<Person> commonConnections= resultProfile.relationToViewer.relatedConnections.personList