This article assumes that you already have a LinkedIn account. The OAuth integration in the example is made using PHP and Javascript is used on browser side.
After that you have to verify your App. By clicking on verify button as below
After clicking on button, a popup will appear. Click on Generate URL to verify your app.
Copy the URL and open in the new tab. Click on Verify button. If your app is verfied successfully then you will get a message that you are verified successfully. To continue further, click on Continue to LinkedIn Developers.
Under the Auth section->OAuth 2.0 settings , enter Authorized redirect URLs for your app. On this URL, the linkedin will redirect to this URL on successful authentication of the user.
For this example, I have created a button through html code
<button onclick="redirectMe()">LinkedIn</button>
function redirectMe(){
window.location.href = "https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id={your_client_id}&redirect_uri="+encodeURIComponent('{your_callback_url}')+"&state=SignupAuth&scope="+encodeURIComponent("r_liteprofile r_emailaddress w_member_social");
}
Param Name | Param Value |
---|---|
client_id | Paste the Client ID that you get from step 1 point 4 |
redirect_uri | Enter the same redirect_uri which you added in Step 1 point 6. |
state | Enter any unique name here |
scope | User information you want to access r_liteprofile , r_emailaddress , w_member_social |
Create a PHP File at the location of your Redirect URL. LinkedIn will send the auth token to this URL. In the PHP file write the following code.
Retrieve Auth Token given by linkedIn. It is given as a get parameter code
//1. received the auth token
$code = $_GET['code'];
Make a POST Request to fetch the access token. Here to will need you send you authorization code, client id, client secret and the same redirect url.
//2. POST request to fetch the access token
$ch = curl_init('https://www.linkedin.com/oauth/v2/accessToken');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
$redirectUri = urlencode('{your_redirect_uri}');
$gt = urlencode('authorization_code');
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=$gt&code=$code".
"&redirect_uri=$redirectUri&client_id={your_client_id}&client_secret={your_client_secret}");
// execute!
$response = curl_exec($ch);
// close the connection, release resources used
curl_close($ch);
// $response contains
$json = json_decode($response);
$accessToken = $json->access_token;
Now you have the access token, you can make requests to access user information. Here, we are accessing user data like profile picture, first name and last name.
// 3. GET request for user data (does not contain email) using accessToken in Authorization header
$url = 'https://api.linkedin.com/v2/me?projection=(id,localizedLastName,localizedFirstName,profilePicture(displayImage~:playableStreams))';
$crl = curl_init();
curl_setopt($crl, CURLOPT_URL, $url);
curl_setopt($crl, CURLOPT_FRESH_CONNECT, true);
curl_setopt($crl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($crl, CURLOPT_HTTPHEADER, array("Authorization: Bearer ".$accessToken));
$userDataJson = curl_exec($crl);
$userData = json_decode($userDataJson,true);
$userName = $userData['localizedFirstName'].' '.$userData['localizedLastName'];
$userProfilePic = $userData['profilePicture']['displayImage~']['elements'][0]['identifiers'][0]['identifier'];
curl_close($crl);
If you want to access the user email, you can make the following request.
// 4. GET request for user email
$email = 'https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))';
$emailcrl = curl_init();
curl_setopt($emailcrl, CURLOPT_URL, $email);
curl_setopt($emailcrl, CURLOPT_FRESH_CONNECT, true);
curl_setopt($emailcrl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($emailcrl, CURLOPT_HTTPHEADER, array("Authorization: Bearer ".$accessToken));
$email_response = curl_exec($emailcrl);
$userEmail = json_decode($email_response,true);
$userEmail = $userEmail['elements'][0]['handle~']['emailAddress'];
curl_close($emailcrl);