Integration with LinkedIn.com Part 1

If your site is a business-oriented community, consider integrating it with LinkedIn.com in order to draw targeted, business-minded visitors and members. Given LinkedIn’s focus on business and technical professionals, it is an excellent potential source of social traffic to your site; simply adding an “invite friends” button from LinkedIn will allow you to draw IT and business specialists directly from LinkedIn to your website.

Talking about “integrating” is one thing; how does one integrate in practice? LinkedIn provides its members with a simple, well-documented API to allow for this and other off-site functions. The OAuth protocol, used by LinkedIn to authorize its account holders, allows you to integrate the content you create on LinkedIn with your website without the need to store the account password locally. While working with OAuth is not necessarily a simple thing, free libraries like this one (that I personally use) exist on the web to make the process an easier one.

The first step is to register your site with LinkedIn; this will in turn provide you with the API key and Secret key, each of which is needed to proceed. The keys are used to authorize users in these steps:

1. First we’ll create a temporary token for the user; this will later be used until a permanent access token is generated:

$linkedin = new LinkedIn($linkedin_api_key, $linkedin_secret_key, $linkedin_callback_url);

$linkedin->getRequestToken();

Here, $linkedin_api_key and $linkedin_secret_key are the keys obtained from LinkedIn upon registering of our application, and $linkedin_callback_url is the url on your site where the user will be redirected after authorizing.

Now a Request token is generated and gets in $linkedin->request_token where it must be retained, for example in a database.

2. Direct the user to a custom URL, formatted with the help of a Request token

header("Location: " . $linkedin->generateAuthorizeUrl());

Here the user will see a form for authorization where they’ll need to enter their email address and password. Following this, the user will be redirected back to your site, specifically to an URL which will be defined when the object is created by LinkedIn.

3. Now we need to create an object called LinkedIn and include within it all of the data necessary to generate an Access Token.

$linkedin = new LinkedIn($linkedin_api_key, $linkedin_secret_key, $linkedin_callback_url);

$linkedin->request_token = '';

$linkedin->oauth_verifier = $_REQUEST['oauth_verifier'];

Here, $ _REQUEST [‘oauth_verifier’] is a variable passed by LinkedIn.

4. Now we can generate the Access Token.

$linkedin->getAccessToken($oauth_verifier);

The code: $ linkedin-> access_token must be stored in the database for future usage.

All of these actions must be completed only during the first connection of a user to LinkedIn from your website; in the future, the same user will be able to connect to LinkedIn using the Access Token in your database:

$linkedin = new LinkedIn($linkedin_api_key, $linkedin_secret_key, $linkedin_callback_url);

$linkedin->access_token = < obtained from the database >;

// obtain data about the user

$xml_response = $linkedin->getProfile("~:(id,first-name,last-name,headline,picture-url)");
This entry was posted in PHP Tips.

Comments are closed.