Saturday, March 26, 2016

Laravel LinkedIn Verification - Laravel LinkedIn Login

Recently in one of the laravel project we have to add LinkedIn verification. It's like we have to auto check if user is signing up on our app should have LinkedIn profile with same name and email. In this blog I am going to explain how to do this. First of all we will need one LinkedIn application. Go to following URL to create new LinkedIn application.

https://www.linkedin.com/developer/apps/

Once you create application it should look like below screenshot.


Now you have to copy client Id and client secret to your laravel app, that you can do in env file.

LINKEDIN_CLIENT_ID = your_client_id
LINKEDIN_CLIENT_SECRET = your_client_secret

Now in your laravel view you have to add a link, so user can click on it and go to LinkedIn for authentication.

Add following to your routes.

    Route::get('/user/linkedin-verification','AdminController@viewLinkedinVerification');

Following should be function of your controller.

        $user_id = Auth::user()->id;
        $user = User::find($user_id);
        $redirect_uri = env('LINKEDIN_REDIRECT_URL', '');
        $client_id = env('LINKEDIN_CLIENT_ID', '');
        $client_secret = env('LINKEDIN_CLIENT_SECRETE', '');
        return view('linkedinverification.index')-              >with(['user'=>$user,'redirect_uri'=>$redirect_uri,'client_id'=>$client_id,'client_secret'=>$client_secret]);

Where user is currently logged in user and redirect URL is the URL that will be invoked on successful LinkedIn verification,

Your view should have a link like below.

                        <p>Please verify your LinkedIn account. <a href="https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id={{$client_id}}&redirect_uri={{$redirect_uri}}&state=ab234aatdssda">Click Here</a></p>

When user click on this link they will see below screen of your LinkedIn application.


Once you are logged in with your LinkedIn account it will redirect back you website with specified redirect url with access code.

Add following to your laravel route.

Route::get('/user-dashboard/linkedin-response','AdminController@linkedinVerification');

In my case I used above URL, you can have different URL as per your requirement.

Now in linkedinVerification function first we will get access code which is generated by LinkedIn and get oAuth access token first. following is the code to get access token.

            $redirect_uri = env('LINKEDIN_REDIRECT_URL', '');
            $client_id = env('LINKEDIN_CLIENT_ID', '');
            $client_secret = env('LINKEDIN_CLIENT_SECRET', '');

            $code = Input::get('code');
            
            /// for get accesstoken
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL,"https://www.linkedin.com/uas/oauth2/accessToken");
            curl_setopt($ch, CURLOPT_POST, 5); // number of post parameters
            curl_setopt($ch, CURLOPT_POSTFIELDS,
                        "code=".$code."&client_id=".$client_id."&redirect_uri=".$redirect_uri."&grant_type=authorization_code&client_secret=".$client_secret);
            
            // receive server response ...
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            
            $server_output = curl_exec ($ch);
            curl_close ($ch);

Now you get access token, use that to get user profile information.

                $result = json_decode($server_output, true);
                
                $access_token = $result['access_token'];
                
                if($access_token != '')
                {
                    // for get customer email
                    $datach = curl_init();  
                    $authorization = 'Authorization: Bearer '.$access_token;
                    curl_setopt($datach,CURLOPT_URL,'https://api.linkedin.com/v1/people/~:(id,email-address,first-name,last-name)?format=json');
                    curl_setopt($datach,CURLOPT_RETURNTRANSFER,true);
                    curl_setopt($datach, CURLOPT_HTTPHEADER, array($authorization));
                 
                    $output=curl_exec($datach);
                 
                    curl_close($datach);
              }

Above call will give you basic user profile, now we just get email and name from it and compare it with credentials registered with us.

Hope this helps you.

No comments:

Post a Comment