The Spring Social LinkedIn project is an extension to Spring Social that enables integration with LinkedIn.

1. Introduction

LinkedIn is a social networking site geared toward professionals. It enables its users to maintain and correspond with a network of contacts they have are professionally linked to.

Spring Social LinkedIn enables integration with LinkedIn with LinkedInConnectionFactory, a connection factory that can be plugged into Spring Social’s service provider connection framework, and with an API binding to LinkedIn’s REST API.

1.1. How to get

The following Gradle dependency will add Spring Social LinkedIn to your project:

build.gradle
compile "org.springframework.social:spring-social-linkedin:1.0.0.RC4"

Or in Maven:

pom.xml
<dependency>
  <groupId>org.springframework.social</groupId>
  <artifactId>spring-social-linkedin</artifactId>
  <version>1.0.0.RC4</version>
</dependency>

As an extension to Spring Social, Spring Social LinkedIn depends on Spring Social. Spring Social’s core module will be transitively resolved from the Spring Social LinkedIn dependency. If you’ll be using Spring Social’s web module, you’ll need to add that dependency yourself. In Gradle:

build.gradle
compile "org.springframework.social:spring-social-web:1.1.0.RC1"

Or in Maven:

pom.xml
<dependency>
  <groupId>org.springframework.social</groupId>
  <artifactId>spring-social-web</artifactId>
  <version>1.1.0.RC1</version>
</dependency>

Note that Spring Social LinkedIn may release on a different schedule than Spring Social. Consequently, Spring Social’s version may differ from that of Spring Social LinkedIn.

Consult Spring Social’s reference documentation for more information on Spring Social dependencies.

2. Configuring LinkedIn Connectivity

Spring Social’s ConnectController works with one or more provider-specific ConnectionFactory instances to exchange authorization details with the provider and to create connections. Spring Social LinkedIn provides LinkedInConnectionFactory, a ConnectionFactory for creating connections with LinkedIn.

So that ConnectController can find LinkedInConnectionFactory, it must be registered with a ConnectionFactoryRegistry. The following configuration class uses Spring Social’s Java configuration support to register a ConnectionFactory for LinkedIn:

@Configuration
public class SocialConfig implements SocialConfigurer {

    @Override
    public void addConnectionFactories(ConnectionFactoryConfigurer cfConfig, Environment env) {
        cfConfig.addConnectionFactory(new LinkedInConnectionFactory(
            env.getProperty("linkedin.clientId"),
            env.getProperty("linkedin.clientSecret")));
    }

    ...
}

If we wanted to add support for connecting to other providers, we would simply register their connection factories here in the same way as LinkedInConnectionFactory.

Because client IDs and secrets may be different across environments (e.g., test, production, etc) it is recommended that these values be externalized. As shown here, Spring’s Environment abstraction is provided as a parameter to addConnectionFactories() so that it can look up the application’s client ID and secret.

Optionally, you may also configure LinkedInConnectionFactory in XML. Using Spring Social LinkedIn’s XML configuration namespace:

<linkedin:config app-id="${linkedin.clientId}"
                 app-secret="${linkedin.clientSecret}"
                 app-namespace="socialshowcase" />

This is roughly equivalent to the Java-based configuration of ConnectionFactoryRegistry shown before. As in the Java-based configuration, the application’s client ID and secret are externalized (shown here as property placeholders).

Refer to Spring Social’s reference documentation for complete details on configuring ConnectController and its dependencies.

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 2 access token to the constructor:

String accessToken = "..."; // The access token granted after OAuth authorization
LinkedIn linkedin = new LinkedInTemplate(accessToken);

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 instance, you can perform a several operations against LinkedIn’s API. The LinkedIn interface is defined as follows:

public interface LinkedIn extends ApiBinding {

  ConnectionOperations connectionOperations();

  NetworkUpdateOperations networkUpdateOperations();

  ProfileOperations profileOperations();

  CompanyOperations companyOperations();

  CommunicationOperations communicationOperations();

  JobOperations jobOperations();

  GroupOperations groupOperations();

  RestOperations restOperations();

}

Each method returns sub-APIs, partitioning the LinkedIn service API into divisions targeting specific facets of LinkedIn functionality. These sub-APIs are defined by interfaces described in LinkedIn’s Sub-APIs.

Table 1. LinkedIn’s Sub-APIs
Sub-API Interface Description

ConnectionOperations

Send and receive connection requests with other LinkedIn users.

NetworkUpdateOperations

Retrieve and perform network updates (status updates).

ProfileOperations

Retrieve and update user profiles.

CompanyOperations

Retrieve and update company profiles.

CommunicationOperations

Send messages.

JobOperations

Search, retrieve and bookmark job opportunities.

GroupOperations

Work with LinkedIn groups.

In addition to the LinkedIn-specific sub-APIs described in table LinkedIn’s Sub-APIs, LinkedIn also has a restOperations() method that returns a RestOperations (e.g., RestTemplate). The RestOperations returned is instrumented to add an OAuth Authorization header for all requests it sends to LinkedIn.

What follows is a brief survey of common tasks you may perform with LinkedIn and its sub-APIs. For complete details on the Spring Social’s entire LinkedIn API binding, refer to the JavaDoc.

3.1. Retrieving a user’s LinkedIn profile data

To retrieve the authenticated user’s profile data, call the getUserProfile() method from the ProfileOperations interface:

LinkedInProfile profile = linkedin.profileOperations().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.profileOperations().getProfileId();

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

String profileUrl = linkedin.profileOperations().getProfileUrl();

3.2. Getting a user’s LinkedIn connections

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

List<LinkedInProfile> connections = linkedin.connectionOperations().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).