LinkedIn is a business oriented social networking platform and used for professional networking. We create a tutorial on login with LinkedIn oAuth which is not working properly and difficult to configure for developers, after receiving many complains from rea
Let’s start.
Step 1: Goto https://www.linkedin.com/developer/apps/ and click Create Application button.
Step 2: Fill all information of your application.
Press Submit button it will redirect you to the application page.
Step 3: Add callback URL on application page.
Press Update and you are done with application settings.
Now Move the the database section
Create users table follow structure below:
ders I am writing this new tutorial on LinkedIn oAuth2 it’s super easy to integrate with your website in few simple steps.
--
-- Database: `linkedin`
--
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userid` varchar(30) NOT NULL,
`firstName` varchar(100) NOT NULL,
`lastName` varchar(100) NOT NULL,
`emailAddress` varchar(100) NOT NULL,
`position` varchar(200) NOT NULL,
`location` varchar(40) NOT NULL,
`profileURL` varchar(200) NOT NULL,
`pictureUrls` text NOT NULL,
`headline` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
PHP Code
db.php
<?php
define('DB_SERVER', 'localhost'); // Database server
define('DB_USERNAME', 'username'); // Database Username
define('DB_PASSWORD', 'password'); // Database Password
define('DB_DATABASE', 'database'); // Database Name
$connection = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE); // connecting with database
?>
Edit above file as per your database configuration.
config.php
<?php
$config['callback_url'] = ''; //Your callback URL
$config['Client_ID'] = ''; // Your LinkedIn Application Client ID
$config['Client_Secret'] = ''; // Your LinkedIn Application Client Secret
?>
Edit config file and add you application information. check application page step 2.
index.php
Include config.php and db.php files in index file
<?php
require_once('config.php');
require_once('db.php');
?>
Below code will verify your configuration.
<?php
if ($config['Client_ID'] === '' || $config['Client_Secret'] === '') {
echo 'You need a API Key and Secret Key to test the sample code. Get one from <a href="https://www.linkedin.com/developer/apps/">https://www.linkedin.com/developer/apps/</a>';
exit;
}
?>
Login button section
<?php
echo '<a href="https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id='.$config['Client_ID'].'&redirect_uri='.$config['callback_url'].'&state=98765EeFWf45A53sdfKef4233&scope=r_basicprofile r_emailaddress"><img src="./images/linkedin_connect_button.png" alt="Sign in with LinkedIn"/></a>';
?>Callback code section on index.php file
<?php
if(isset($_GET['code'])) // get code after authorization
{
$url = 'https://www.linkedin.com/uas/oauth2/accessToken';
$param = 'grant_type=authorization_code&code='.$_GET['code'].'&redirect_uri='.$config['callback_url'].'&client_id='.$config['Client_ID'].'&client_secret='.$config['Client_Secret'];
$return = (json_decode(post_curl($url,$param),true)); // Request for access token
if($return['error']) // if invalid output error
{
$content = 'Some error occured<br><br>'.$return['error_description'].'<br><br>Please Try again.';
}
else // token received successfully
{
$url = 'https://api.linkedin.com/v1/people/~:(id,firstName,lastName,pictureUrls::(original),headline,publicProfileUrl,location,industry,positions,email-address)?format=json&oauth2_access_token='.$return['access_token'];
$User = json_decode(post_curl($url)); // Request user information on received token
// Insert Data in Database
$query = "INSERT INTO `test`.`users`
(`userid`,
`firstName`,
`lastName`,
`emailAddress`,
`position`,
`location`,
`profileURL`,
`pictureUrls`,
`headline`)
VALUES
('$id',
'$firstName',
'$lastName',
'$emailAddress',
'$position',
'$location',
'$profileURL',
'$pictureUrls',
'$headline')";
mysqli_query($connection,$query);
}
}
?>
First off all we check if code received or not, using that code we will request for access_token once we get token we can get users information like name, email, picture and many more. In last step store that data in database.
That’s it all done and you have a output looks like this: