Apparently, it’s not easy for beginners to get started with OAuth protocol and tedious authentication/authorization process. In this article, we’re going to simplify LinkedIn Login integration as much as possible. Alright, so here is the basic overview of what we’re going to do
- Procedure / How it works
- Creating LinkedIn app
- Communicating with LinkedIn
- Getting user details
- Going Live
How it works
- Our webpage will send user to LinkedIn site so he/she can authorize permissions.
- After that, user will be brought back to your site with success or failure information.
- We will then store the required information in our database or use however we want.
Creating LinkedIn App
You are required to have a LinkedIn account in order to create a LinkedIn APP. So once you’re done with that, visit this link and create a new application. After that you’ll have the app key and app secret, which is necessary to integrate LinkedIn login feature on your php website. So, copy that information so we can use it later in this article. Also make sure you add your webpage ( the page which is going to start the communication, suppose linkedin_login.php ) url to list of
Communicating with LinkedIn
LinkedIn allow us to use both OAuth2.0 and OAuth1.0a, in this article we’re going to use OAuth2. We’ll have two different files to make it simple to understand, linkedin_login.php and linkedin_callback.php. Also we’ll assume the domain name https://example.com. At user user will visit the http://example.com, here is the code for that
linkedin_login.php
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php $app_key = "your-app-key"; // From LinkedIn App Dashboard $app_secret = "your-secret-key"; // From Linkedin APP Dashboard $get_parameters = array( "response_type" =>"code", // Required & Hardcorded "redirect_uri" => "http://example.com/linked_callback.php", "client_id" => $app_key, "state" => md5(rand(0,500).date("d-y-m h:i:s")) // Some random unique code. Linkedin is going to send this back to us, so we can make sure data was not tampered in mid way. ); $auth_url = "https://www.linkedin.com/oauth/v2/authorization?".http_build_query($get_parameters); // Simply adding those GET parameters header("Location: ".$auth_url); // Redirecting user to auth_url, so user can authorize. ?> |
Now user will leave your site i.e. http://example.com and visit linkedin for authorization, if everything went good, user will be redirected to http://example.com/linked_callback.php here it is:
linked_callback.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<?php $app_key = "your-app-key"; // From LinkedIn Dashboard $app_secret = "your-app-secret"; // From LinkedIn Dashboard if(isset($_GET['code'])){ // Authorization was successful, now we can ask for access token. $code = $_GET['code']; // Preparing information for query parameters. $get_param = array( "grant_type" =>"authorization_code", "code" => $code, "redirect_uri" => "http://example.com/linkedin_callback.php", // Pass the same url as given in linkedin_login.php as we won't be redirecting user again. "client_id" => $app_key, "client_secret" => $app_secret ); // Making GET call for accesstoken. $result = json_decode(file_get_contents("https://www.linkedin.com/oauth/v2/accessToken?".http_build_query($param))); // Getting output of that page. // Fetching information. $access_token = $result->access_token; $expires_in = $result->expires_in; // Now we've the access token, we're ready to make all the API calls to LinkedIn. // Making another call to get firstname, email and lastname. $url = "https://api.linkedin.com/v1/people/~:(firstName,lastName,email-address)?oauth2_access_token=".$access_token."&format=json"; $user = json_decode(file_get_contents($url)); if(!isset($user->emailAddress)) die("Something went wrong, couldn't get the page result."); // Here we have the user email, firstname an dlastname in $user object // To continue with sign in, here is basic procedure /* Basic pseudo code if($user->emailAddress doesn't exists in database){ createNewAccount(); }else{ loginAccountWithEmail($user->emailAddress); } */ }else{ echo "Something went wrong, either app_key or app secret was incorrect."; } ?> |
Going Live
You may have noticed that only your email address is working with linkedin login, not others. The reason behind this is your APP Mode. By default LinkedIn application is under development mode. So, if you want to make the LinkedIn login active for other users, you need to set it to Live by going into Application Dashboard > Settings. I can’t provide the URL as app id will be different for each app. However URL will look something like https://www.linkedin.com/developer/apps/-somecode-/settings
That’s it, enjoy your feature!
Feel free to leave comment for question or concerns.