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.
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
<?php $config['base_url'] = 'http://demo.phpgang.com/'; $config['callback_url'] = 'http://demo.phpgang.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.
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?
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
You are in list now 🙂
In my case no token is generated https://api.linkedin.com/uas/oauth/authorize?oauth_token=
Please help
Your server time and Linkedin time sync issue.
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??
In method: getAccessToken (class: linkedinAuth.php), $response is empty:
$response = $this->httpRequest($url, $headers, “GET”);
print_r($response) = nothing.
Who know why? Thx =)
Sync your server time with LinkedIn server.
my token doesn’t exist …?
any other way to download the code
Dear Uday downloading email list take 18 hours to update and now list updated you can download code.
I already sign up my email to download, but still I can’t download.. help
@jolsbardz:disqus List update after every 18 hours now you are in the list and go to download scrip.
Feel free to contact us.
When i have login with linkdin application does not fetch email id or user info and does not create it wordpress user…
What error you are getting in your script please paste below.
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…..
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.
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.
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}
)
)
)
Hello
How i can got the user profile for create it wordpress user…..
To insert data in wordpress you have to create a plugin which perform this task for you.
Hi this a amazing application, thanks.
I just wanna know how to get the fields’ titles?
Hi this a amazing application, thanks.
I just wanna know how to get the fields’ titles?
Please Can someone help me to find out how to retrieve fields’ titles?
@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.
Thanks Huzoorbux,
I’m going to try it right now and give a feedback asap.
Awsome,
Thanks Huzoorbux, the array is very simple to manipulate.
I appreciate your help.
code not working getting error please help me sir “We were unable to find the authorization token”
What errors you are getting please post your error so we can debug it.
getting error like The requested URL /linkedin_login_oauth/linkedinauth.php was not found on this server.
Check your paths
i am also getting the same error, did u get any solution
i want to insert the linked in details to database . how can i do that?
When you receive data you have to put you database queries there and insert data in database.
could you tell me where i can write the query in the given source code because i am not familiar with this auth integration.
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 ?
Change your server time to sync with linkedin.
How do we do that ? New to all this.
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.
Thanks a lot 😀
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.
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.
token nor returning
Debug error before redirecting to linkedin it might be time syncing error.
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=”
before that scrip generate a token so you have to stop redirection and show errors and fix it.
getting $linkedin->getRequestToken(); blank
hi,
Notice: Use of undefined constant CONSUMER_SECRET – assumed ‘CONSUMER_SECRET’ in C:xampphtdocslinkedin_login_oauthindex.php on line 3
Sir,
Can you please tell me how can i set the logo?
Thanks
Deepak
I see for this and there is no option in the app setting.
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.
Many thanks Sir.
I got it.
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?
Showing error
Call to undefined method SimpleXMLElement::count() in linkedin_login_oauth/linkedinauth.php on line 35
An Error occurred during authorization, please try again later.where do i need to change the server time so that this code can work
Fatal error: Call to undefined function curl_init() in C:wampwwwlinkedin_login_oauthlinkedin_login_oauthoAuthlinkedinoAuth.php on line 117Call Stack
plz help me
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
use some free hosting to test it on localhost it gives many errors.
thank you
i have the same error
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.
when i am clicking on cancel button it is redirect on index page. How do i remove this bug?
i cant download script
Fatal error: Call to undefined method SimpleXMLElement::count()
Fatal error: Call to undefined method SimpleXMLElement::count() in linkedinauth.php
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 .
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.
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)
I am getting this error : – An Error occurred during authorization, please try again later.
Ccn you please help me to figure out the issue?
same Problem
same here bro…..?
Hi ,this code not working for me.I have following error
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
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 ?
plz help me… https://uploads.disquscdn.com/images/0f092151364630c76a025c2f504a4e8982f8ac00fe10937ecfa90115368772ae.png
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.
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.
Please see my reply above. This should solve your issue.
Looks like my comment has to be approved.. It was kinda lengthy with a photo attached.
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!
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
You’re very welcome 🙂
You’re very welcome 🙂
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.
Please check our new tutorial here: https://www.phpgang.com/how-to-login-with-linkedin-oauth2-in-php-and-mysql_3858.html