Reading:
LinkedIn OAuth Signin using PHP

LinkedIn OAuth Signin using PHP

Metamug
LinkedIn OAuth Signin using PHP

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.

Step 1: Get Client ID & Client Secret key

  1. Go to linkedIn developers page, create your account.

Create linkedin developers account

  1. After signin successfully, click on Create app button. You will see the Create an App screen where you have put all the details.

fill the details for linkedin oauth integration

  1. After that you have to verify your App. By clicking on verify button as below verify your app

  2. After clicking on button, a popup will appear. Click on Generate URL to verify your app. click on generate url

  3. 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.

  4. 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. click on generate url

  5. Under the same section, you will find Client ID and Client Secret. Copy these as it will need in next step. client id and client secret key

Step 2: Create a linkedIn Button

  1. For this example, I have created a button through html code

    <button onclick="redirectMe()">LinkedIn</button>
  2. Now, using Javascript we will give onclick action to the 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 PHP File to get access_token

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.

  1. Retrieve Auth Token given by linkedIn. It is given as a get parameter code

    //1. received the auth token
    $code =  $_GET['code'];
  2. 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;
  3. 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);
  4. 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);
  5. You can now send this data to your own server and log in the user.

    If you find this article helpful then please do share it. It may help others. Happy coding :))


Icon For Arrow-up
Comments

Post a comment