Thursday, 10 July 2014

How to Integrate login with LinkedIn oAuth in PHP


Now a days popular sites provide api so that we can integrate in our sites and get there information on one click no more fill complex forms etc. Today i am going to explain you how to integrate with LinkedIn one of the most popular website.

Make New Application on LinkedIn:

1.visit https://www.linkedin.com/secure/developer and click Add New Application.

Fill all information of your application.

Company Info
-------------
Company :
  Company Name :
  Account Administrator :

Application Info
----------------
       Application Name :
            Description :
        Integration URL :
       Application Type :
            Live Status :


Now, You'll get linkedin API key and Secret Key

Programming Time:
The script contains two folders called oAuth and images with PHP files.

oauth
- class.linkedClass.php // Class Get user Information
- config.php // Configuration file
- linkedinoAuth.php // Call methods
- OAuth.php // Twitter OAUTH library

images

linkedinauth.php //Call back page Show details
html.inc // html design view
index.php // Main index file show data
linkedin.php // Redirect to linkedin for authorization

PHP Code
---------

<?php
$config['base_url']             =   'http://demo.phptipsntricks.com/';
$config['callback_url']         =   'http://demo.phptipsntricks.com/index.php';

//linkedin configuration
$config['linkedin_access']      =   'your_linkedin_access';
$config['linkedin_secret']      =   'your_linkedin_secret';
$config['linkedin_library_path']=   'linkedinoAuth.php';
?>


Index.php
-----------
Shows a button for linkedin authorization if not authorized.

Redirect you to linkedin.php generate token and redirect to linkedin app authorization page.

<?php
    session_start();
    include_once 'oAuth/config.php';
    include_once 'oAuth/linkedinoAuth.php';

    # First step is to initialize with your consumer key and secret. We'll use an out-of-band oauth_callback
    $linkedin = new LinkedIn($config['linkedin_access'], $config['linkedin_secret'], $config['base_url'] . 'linkedin_login_oauth/linkedinauth.php' );
    // $linkedin->debug = true;

    # Now we retrieve a request token. It will be set as $linkedin->request_token
    $linkedin->getRequestToken();
    $_SESSION['requestToken'] = serialize($linkedin->request_token);

    # With a request token in hand, we can generate an authorization URL, which we'll direct the user to
    ## echo "Authorization URL: " . $linkedin->generateAuthorizeUrl() . "\n\n";
    header("Location: " . $linkedin->generateAuthorizeUrl());
?>

Once you Allow access it will redirect you on linkedinauth.php (defined in linkedin.php) and show all data of authorized user.

No comments:

Post a Comment