3. LinkedIn API Binding

Spring Social LinkedIn offers integration with LinkedIn's REST API with the LinkedIn interface and its implementation, LinkedInTemplate.

To create an instance of LinkedInTemplate, you may pass in your application's OAuth 1 credentials, along with an access token/secret pair to the constructor:

String consumerKey = "..."; // The application's consumer key
String consumerSecret = "..."; // The application's consumer secret
String accessToken = "..."; // The access token granted after OAuth authorization
String accessTokenSecret = "..."; // The access token secret granted after OAuth authorization
LinkedIn linkedin = new LinkedInTemplate(consumerKey, consumerSecret, accessToken, accessTokenSecret);

If you are using Spring Social's service provider framework, you can get an instance of LinkedIn from a Connection. For example, the following snippet calls getApi() on a connection to retrieve a LinkedIn:

Connection<LinkedIn> connection = connectionRepository.findPrimaryConnection(LinkedIn.class);
if (connection != null) {
    LinkedIn linkedin = connection.getApi();

    // ... use LinkedIn API binding

Here, ConnectionRepository is being asked for the primary connection that the current user has with LinkedIn. If a connection to LinkedIn is found, it retrieves a LinkedIn instance that is configured with the connection details received when the connection was first established.

Once you have a LinkedIn you can use it to interact with LinkedIn on behalf of the user who the access token was granted for.

3.1 Retrieving a user's LinkedIn profile data

To retrieve the authenticated user's profile data, call the getUserProfile() method:

LinkedInProfile profile = linkedin.getUserProfile();

The data returned in the LinkedInProfile includes the user's LinkedIn ID, first and last names, their "headline", the industry they're in, and URLs for the public and standard profile pages.

If it's only the user's LinkedIn ID you need, then you can get that by calling the getProfileId() method:

String profileId = linkedin.getProfileId();

Or if you only need a URL for the user's public profile page, call getProfileUrl():

String profileUrl = linkedin.getProfileUrl();

3.2 Getting a user's LinkedIn connections

To retrieve a list of LinkedIn users to whom the user is connected, call the getConnections() method:

List<LinkedInProfile> connections = linkedin.getConnections();

This will return a list of LinkedInProfile objects for the user's 1st-degree network (those LinkedIn users to whom the user is directly linked--not their extended network).