LinkedIn OAuth2.0 Authentication Using Rest API and PHP

This PHP tutorial helps to create sign-in and sign-out functionality using LinkedIn rest API. It’s a very common functionality to get user profile information from a linked-in account.

Nowadays, Many websites are using LinkedIn for user signup in career pages. That help to remove the tedious registration process, LinkedIn help to create user profile information with just a single click.

There are the following pre-requisite for LinkedIn Single Sign-in:

  • Create a LinkedIn app from the developer console. You can get more information from Here.
  • After Successfully registering of the app, You will get a ClientId and secret key.
  • Enable mod_ssl from Apache configuration file and enable open_ssl into php.ini file.
  • composer for php packages

I am using LinkedIn rest api to access user information, You can get information from Here.

I will use third party library Happyr LinkedIn API Client for LinkedIn api access.

“A PHP library to handle authentication and communication with LinkedIn API. The library/SDK helps you to get an access token and when authenticated it helps you to send API requests.”

The Happyr LinkedIn library have following features

  • Flexible and easy to extend.
  • Developed with modern PHP standards.
  • Not developed for a specific framework.
  • Handles the authentication process.
  • Respects the CSRF protection.

There are following files will participate into this Project:

  • composer.json: This file will have all dependencies libs information.
  • index.php: This file will have HTML view.
  • linkedin_oauth_config.php This file will have all authenticated related code.

Login with LinkedIn using PHP

We will create an sample project under /htdocs folder, that will have all files of this project. We will create a composer.json file and the below code into this file.

{
    "require": {
        "php-http/curl-client": "^1.7",
        "guzzlehttp/psr7": "^1.4",
        "php-http/message": "^1.6",
        "happyr/linkedin-api-client": "^1.0",
        "php-http/guzzle6-adapter": "^1.1"
    }
}

Above json structure will have all dependencies libs that will use into this tutorial.

Now we will open command line and run composer install command.

$project_path> composer install

We will create index.php file that will use to display HTML view,This page will display LinkedIn login button. We are also displaying success message once user has been authenticated.

<?php
include_once('linkedin_oauth_config.php');
?>
<div class="container">
<h2>PHP Linked-in OAuth 2.0 Login</h2>
<div class="well"><!--?php if (isset($linkedInAuthUrl)): ?--><form action="<?php echo $linkedInAuthUrl; ?>" method="get"><a href="<?php echo $linkedInAuthUrl; ?>"> <img class="resource-paragraph-image lazy-load lazy-load-src" src="https://content.linkedin.com/content/dam/developer/global/en_US/site/img/signin-button.png" alt="Sign in with LinkedIn"> </a></form><!-- Show User Profile otherwise--> <!--?php else: ?-->
<h3>Successfully! Authenticated, Welcome <!--?php echo $userData['firstName'] .' '.$userData['lastName'] ?--></h3>
<a class="btn btn-danger" href="?logout=true">Logout</a> <!--?php endif ?--></div>
</div>

We are checking if access token is not set then we will show login button, otherwise display success message and logged-in user full name.

You have noticed in index.php file, We are using linkedin_oauth_config.php file at the top of the file. This file used to call LinkedIn rest api and authenticate user, So now create linkedin_oauth_config.php file and put the below code into this file.

clearStorage();
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); //redirect user back to page
}

//Set Access Token to make Request
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  $linkedIn->setAccessToken($_SESSION['access_token']);

}

//play with user data
if ($linkedIn->isAuthenticated()) {
  $userData = $linkedIn->get('v1/people/~:(firstName,lastName)');
  $_SESSION['access_token'] = (string) $linkedIn->getAccessToken();
} else {
  $linkedInAuthUrl  =  $linkedIn->getLoginUrl();
}
?>

We have created linked-in login url that will use to authenticate user, Also checking if user is logged-in and session had token then don’t need to again to validate user.

We have also added logout user functionality that cleared the all data from session.

Leave a Reply

Your email address will not be published.