November 26, 2012 6:39 pm

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.

LinkedIn

Follow new Tutorial: How to login with LinkedIn oAuth2 in PHP and MySQL

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

Fill all information of your application.

 

You will 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

Edit config.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.

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

 

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.


Tutorial Categories:
  • I want download source code but i can’t because when i type and press subscribe, after that i come back to download page and when i clicked download message says: Sorry no email found subscribe below. And also where can i know about my email id?

    • Huzoor Bux

      The Subscribers list takes 18 hours to update.

  • the problem is solved, i downloaded already

    • How u have downloaded ?? can you please let me know? as i have no 18 hours so please help me

      • Huzoor Bux

        You are in list now 🙂

  • ahsan

    In my case no token is generated https://api.linkedin.com/uas/oauth/authorize?oauth_token=
    Please help

    • Huzoor Bux

      Your server time and Linkedin time sync issue.

  • Yoyo

    Hi, sorry for my english, but i have a problem, my token doesn’t exist …
    This is my linkedin Object:

    LinkedIn Object
    (
    [base_url] => http://api.linkedin.com
    [secure_base_url] => https://api.linkedin.com
    [oauth_callback] => xxxxx
    [consumer] => OAuthConsumer Object
    (
    [key] => xxxxxxxxxxxx
    [secret] => xxxxxxxxxx
    [callback_url] => xxxxx
    )

    [request_token] =>
    [access_token] =>
    [oauth_verifier] =>
    [signature_method] => OAuthSignatureMethod_HMAC_SHA1 Object
    (
    )

    [request_token_path] => https://api.linkedin.com/uas/oauth/requestToken?scope=r_fullprofile r_emailaddress r_contactinfo
    [access_token_path] => https://api.linkedin.com/uas/oauth/accessToken
    [authorize_path] => https://api.linkedin.com/uas/oauth/authorize

    Someone can help me please??

    • Yoyo

      In method: getAccessToken (class: linkedinAuth.php), $response is empty:

      $response = $this->httpRequest($url, $headers, “GET”);

      print_r($response) = nothing.

      Who know why? Thx =)

      • Huzoor Bux

        Sync your server time with LinkedIn server.

    • Huzoor Bux

      my token doesn’t exist …?

      • uday

        any other way to download the code

        • Huzoor Bux

          Dear Uday downloading email list take 18 hours to update and now list updated you can download code.

  • Jols Bardz

    I already sign up my email to download, but still I can’t download.. help

    • huzoorbux

      @jolsbardz:disqus List update after every 18 hours now you are in the list and go to download scrip.

      Feel free to contact us.

  • Hery

    When i have login with linkdin application does not fetch email id or user info and does not create it wordpress user…

    • huzoorbux

      What error you are getting in your script please paste below.

      • Hery

        Sir

        After login with linkdin the website return on this

        link:-www.demo.com/linkedin_login_oauth/linkedinauth.php?oauth_token=d3dd2e7c-b584-4115-bc52-81e895dff24b&oauth_verifier=78846..

        But i want to know login user name ,password and email to register it with wordpress…..

        • huzoorbux

          send me the page link where you put your linkedin button and please make sure that you have changed you application ids in config.php.

          • Hery

            sir

            I have try to solve the problem but i got “unauthorized” message after login with linkdin:-

            401
            1379409071935
            YT9I6MG03L
            0
            [unauthorized]. The token used in the OAuth request is not valid.

          • sunil

            sir

            I have try to solve the problem

            Array
            (
            [error] => Array
            (
            [status] => Array
            (
            [value] => 400
            )

            [timestamp] => Array
            (
            [value] => 1408517529237
            )

            please give me solution as fasr as u can

            [request-id] => Array
            (
            [value] => 3MLDYSR4PE
            )

            [error-code] => Array
            (
            [value] => 0
            )

            [message] => Array
            (
            [value] => Malformed path segment {:(people:(id,first-name,last-name,picture-url,site-standard-profile-request,headline),num-results)https://api.linkedin.com/v1/people-search}
            )

            )

            )

  • Hery

    Hello

    How i can got the user profile for create it wordpress user…..

    • huzoorbux

      To insert data in wordpress you have to create a plugin which perform this task for you.

  • sayf

    Hi this a amazing application, thanks.
    I just wanna know how to get the fields’ titles?

  • sayf

    Hi this a amazing application, thanks.
    I just wanna know how to get the fields’ titles?

  • sayf

    Please Can someone help me to find out how to retrieve fields’ titles?

    • huzoorbux

      @disqus_6zfMYmlyRQ:disqus it return data in xml you have to parse xml or convert it to array chose the easy steps. I have updated code please download fresh copy and use it now it will return data in array.

      • sayf

        Thanks Huzoorbux,
        I’m going to try it right now and give a feedback asap.

      • sayf

        Awsome,
        Thanks Huzoorbux, the array is very simple to manipulate.

        I appreciate your help.

  • siddhu

    code not working getting error please help me sir “We were unable to find the authorization token”

    • huzoorbux

      What errors you are getting please post your error so we can debug it.

  • binil billu

    getting error like The requested URL /linkedin_login_oauth/linkedinauth.php was not found on this server.

    • huzoorbux

      Check your paths

    • neha

      i am also getting the same error, did u get any solution

  • binil billu

    i want to insert the linked in details to database . how can i do that?

    • huzoorbux

      When you receive data you have to put you database queries there and insert data in database.

      • binil billu

        could you tell me where i can write the query in the given source code because i am not familiar with this auth integration.

  • Ajitesh

    https://www linkedin com/uas/oauth/authorize?oauth_token=

    We were anable to find authorization token. How to fix this ?

    If it is about hte server time and linkedin time sync problem, the how do we fix that ?

    • huzoorbux

      Change your server time to sync with linkedin.

      • Ajitesh

        How do we do that ? New to all this.

    • huzoorbux

      open linkedin.php
      in the httpRequest function add:
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);

      I also changed ubuntu timezone to EST using follow command:

      exec(“ln -sf /usr/share/zoneinfo/EST localtime”); // run this command in a new file and run on your server.

      This erros should be fixed and linkedin should return request token.

      • Ajitesh

        Thanks a lot 😀

      • Anonymous

        Can you tell me how can I store the data retrieved in a database ? I am able to store all elements except for skills field.

  • Manisha Gaidhane

    i am getting error “Notice: Use of undefined constant CONSUMER_SECRET – assumed ‘CONSUMER_SECRET’ in D:xampphtdocslinkedin_login_oauthindex.php on line 3” please suggest me way to solve this.

  • Narasimha

    token nor returning

    • huzoorbux

      Debug error before redirecting to linkedin it might be time syncing error.

      • Manisha Gaidhane

        hello sir, i am also getting same error “An Error occurred during authorization, please try again later.” and url like “linkedincom/uas/oauth/authorize?oauth_token=”

        • huzoorbux

          before that scrip generate a token so you have to stop redirection and show errors and fix it.

          • Manisha Gaidhane

            getting $linkedin->getRequestToken(); blank

  • Sree Hari

    hi,
    Notice: Use of undefined constant CONSUMER_SECRET – assumed ‘CONSUMER_SECRET’ in C:xampphtdocslinkedin_login_oauthindex.php on line 3

  • Deepak

    Sir,

    Can you please tell me how can i set the logo?

    Thanks
    Deepak

    • Deepak

      I see for this and there is no option in the app setting.

    • huzoorbux

      when you click on Add new Application on that page
      OAuth User Agreement area ->App Logo Secure URL: in that text box add your logo url.

      • Deepak

        Many thanks Sir.

        I got it.

  • Marc

    Downloaded the exact Demo coded linked here on the website and getting this error:
    Notice: Use of undefined constant CONSUMER_SECRET – assumed ‘CONSUMER_SECRET’ in C:xampphtdocslinkedin_login_oauthindex.php on line 3
    And if I press on the login button I’m not getting a token. Any way to fix this?

  • Prashant Pawar

    Showing error
    Call to undefined method SimpleXMLElement::count() in linkedin_login_oauth/linkedinauth.php on line 35

  • saurav daga

    An Error occurred during authorization, please try again later.where do i need to change the server time so that this code can work

  • taskeen

    Fatal error: Call to undefined function curl_init() in C:wampwwwlinkedin_login_oauthlinkedin_login_oauthoAuthlinkedinoAuth.php on line 117Call Stack
    plz help me

  • oussama

    HI @huzoorbux:disqus please can you help me i have this error
    Fatal error: Call to undefined method SimpleXMLElement::count() in C:wampwwwphp_linkedin1linkedinauth.php on line 43

    • huzoorbux

      use some free hosting to test it on localhost it gives many errors.

      • oussama

        thank you

      • oussama

        i have the same error

  • wasim pathan

    Array
    (
    [error] => Array
    (
    [status] => 401
    [timestamp] => 1399898567394
    [request-id] => IFLFGOB9CB
    [error-code] => 0
    [message] => [unauthorized]. No consumer found for key /
    )

    )

    When I am integration your code with my Yii application it is giving me the above array in response.
    Using the same key and secret when I run your code it works properly please help.

  • Niranjan Singh Chouhan

    when i am clicking on cancel button it is redirect on index page. How do i remove this bug?

  • minu

    i cant download script

  • Xuan Vichia

    Fatal error: Call to undefined method SimpleXMLElement::count()

  • Ravi Singh

    Fatal error: Call to undefined method SimpleXMLElement::count() in linkedinauth.php

  • Vanishri

    Hi,
    I am getting below output

    Array
    (
    [error] => Array
    (
    [status] => 401
    [timestamp] => 1412938920976
    [request-id] => QCS4ADRKM1
    [error-code] => 0
    [message] => [unauthorized]. The token used in the OAuth request is not valid. consumerKey: 75s75k5nxrkvxp
    )

    )

    Please help me what might me the reason .

  • binil billu

    hi when i am implement the code getting error like below
    An Error occurred during authorization, please try again later. and the code failed to generate the oauth token.

    is there any problem with the http/https iam testing with my local server.
    please help.

    • Brandon DeWitt

      If you’re still interested, take a look at my reply comment under @huzoorbux:disqus’s last comment about 2 weeks ago (should be at the top of this post’s thread)

  • Sushil Kumar

    I am getting this error : – An Error occurred during authorization, please try again later.

    Ccn you please help me to figure out the issue?

    • Mayank

      same Problem

    • Jadav Mehul

      same here bro…..?

  • Deena

    Hi ,this code not working for me.I have following error

  • Manoj Rammurthy

    hello i am able to generate all the required fields , so if i have to make another call with the same linkedin credential how to make it , how to store access token from your library please do the favour

  • kamal

    can any body tell me how to set these files
    and where to place which file in which folder of codigniter
    can anybody help that how and which file will placed into which folder ?

  • Jadav Mehul
    • open linkedin.php
      in the httpRequest function add:
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);

      I also changed ubuntu timezone to EST using follow command:

      exec(“ln -sf /usr/share/zoneinfo/EST localtime”); // run this command in a new file and run on your server.

      This erros should be fixed and linkedin should return request token.

      • Jadav Mehul

        see sir..

    • open linkedin.php
      in the httpRequest function add:
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);

      I also changed ubuntu timezone to EST using follow command:

      exec(“ln -sf /usr/share/zoneinfo/EST localtime”); // run this command in a new file and run on your server.

      This erros should be fixed and linkedin should return request token.

  • Brandon DeWitt

    Please see my reply above. This should solve your issue.

  • Brandon DeWitt

    Looks like my comment has to be approved.. It was kinda lengthy with a photo attached.

  • Brandon DeWitt

    After going a little further into the code – since I continued to get error messages. I noticed that in the same file (oAuth/linkedinoAuth.php) there were a couple of php errors that said a reference variable ‘debug’ was not set.

    Change lines 57 & 73 to read if($this->debug), rather than just if($debug). For whatever reason, even though it was listed as a public variable at the beginning of the class, it wasn’t pulling through. This seems to have fixed this issue.

    I think the wise choice for folks who are looking to use this is to store the token & secret that you receive from LinkedIn in a database of some kind or a session variable, because if you refresh the linkedinauth.php page after you’ve already logged in, it no longer has a trace of the token & secret – its only available the first time this page processes/loads and then you’ll have to go back to the log in page an do log in again. This causes two errors for unset variables ‘oauth_token’ & ‘oauth_token_secret’, which again would not be thrown if the script checked a DB or Session variable and if not set, redirect back to login page.

    I also kept getting an error on line 121 in oAuth/linkedinoAuth.php – You have a comment listed as // Set the headers. Anything that I try and change this too, it kills the login and return of user information. I simply put the @ symbol in front of the line and kept it moving. Now it seems the script is working exactly as it should! 🙂

    @huzoorbux:disqus you’re the man! Even though I’ve had to troubleshoot the code a bit, this is a great script!

    • Brandon DeWitt

      You already have session variables! Awesome! 🙂

      But in the linkedinauth.php file line 13 should read:

      if (isset($_REQUEST[‘oauth_verifier’]) AND !isset($_SESSION[‘oauth_access_token’])){

      rather than:

      if (isset($_REQUEST[‘oauth_verifier’])){

      Or it just tries to get another access token, but LinkedIn doesn’t like this and throws this error with the variable returned: [message] => [unauthorized]. The token used in the OAuth request is not valid.

      • Thank you @brandon_dewitt:disqus 🙂 you solved most of the issues

        • Brandon DeWitt

          You’re very welcome 🙂

        • Brandon DeWitt

          You’re very welcome 🙂

  • karthick

    I am getting this error An Error occurred during authorization, please try again later.when integrating linkedin signup in website(localhost).Please provide solution asap.