How to login with linkedin on website? Reviewed by Momizat on . First creat a Application on linkedin go to:- https://www.linkedin.com/secure/developer Click on the Add New Application then open form and fill all details. PH First creat a Application on linkedin go to:- https://www.linkedin.com/secure/developer Click on the Add New Application then open form and fill all details. PH Rating: 0

How to login with linkedin on website?

First creat a Application on linkedin go to:-

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

Screen Shot 2014-05-01 at 14.27.40

Click on the Add New Application then open form and fill all details.

Screen Shot 2014-05-01 at 14.26.41

PHP code linkedin-login.php on website


define('API_KEY',"xxxxxxxxxxx");
define(API_SECRET',"xxxxxxxxxxxxx");
define('REDIRECT_URI','http://www.site.com/linkedin-login.php');
define('SCOPE','r_fullprofile r_emailaddress rw_nus ');
// OAuth 2 Control Flow
$res=array();
if (isset($_GET['error'])) {
	// LinkedIn returned an error
	print $_GET['error'] . ': ' . $_GET['error_description'];
	exit;
} elseif (isset($_GET['code'])) {			
	 $this->getAccessToken();
	 $state = $_session['state'];
	 
	if ($state==$_GET['state']) {
		// Get token so you can make API calls
		$this->getAccessToken();
	} else {
		// CSRF attack? Or did you mix up your states?
		//echo "CSRF attack";
	   
	}
} else { 
$expires_at = $_session['expires_at'];
$access_token = $_session['access_token'];
	if ((empty($expires_at)) || (time() > $expires_at)) {				
		$_SESSION = array();
	}
	if (empty($access_token)) {
		// Start authorization process
		$this->getAuthorizationCode();
	}
}
$user = $this->fetch('GET', '/v1/people/~:(id,firstName,lastName,emailAddress,headline,location,picture-url,date-of-birth)');
//$user =$this->fetch('GET', 'http://api.linkedin.com/v1/people/~?format=json');

if(!empty($user)){
	if(isset($user->emailAddress)) $res['email'] = $user->emailAddress;
	if(isset($user->pictureUrl)) $res['img'] = $user->pictureUrl;
	if(isset($user->firstName)) $res['users_fname']=$user->firstName;
	if(isset($user->lastName)) $res['users_lname']=$user->lastName;			
	$res['id']=$user->id;
	}
	print_r($res);
	
public function getAuthorizationCode(){
	 $params = array('response_type' => 'code',
				'client_id' => API_KEY,
				'scope' => SCOPE,
				'state' => 'testsite', 
				'redirect_uri' => REDIRECT_URI
		  );
		$url = 'https://www.linkedin.com/uas/oauth2/authorization?'.http_build_query($params);
		$_SESSION['state'] = $params['state'];
		header("Location: $url");	
}
	     
function getAccessToken() {
    $params = array('grant_type' => 'authorization_code',
                    'client_id' => "xxxxxxx",
                    'client_secret' => "xxxxxxxxxxxxxxxx",
                    'code' => $_GET['code'],
                    'redirect_uri' => REDIRECT_URI,
              );     
    // Access Token request
  $url = 'https://www.linkedin.com/uas/oauth2/accessToken?'.http_build_query($params);
     
    // Tell streams to make a POST request
    $context = stream_context_create(
                    array('http' => 
                        array('method' => 'POST',
                        )
                    )
                );
 
    // Retrieve access token information
   $response = @file_get_contents($url, true, $context);
 
    // Native PHP object, please
   $token = json_decode($response);
    $_SESSION['access_token'] = $token->access_token; 
    $_SESSION['expires_in']   = $token->expires_in; 
    $_SESSION['expires_at']   = time() + $_SESSION['expires_in'];
    return true;
}
 
function fetch($method, $resource, $body = '') {
	$access_token = $this->_ci->session->userdata('access_token');
    $params = array('oauth2_access_token' => $access_token,
                    'format' => 'json',
              );
     
    // Need to use HTTPS
    $url = 'https://api.linkedin.com' . $resource . '?' . http_build_query($params);
    // Tell streams to make a (GET, POST, PUT, or DELETE) request
    $context = stream_context_create(
                    array('http' => 
                        array('method' => $method,
                        )
                    )
                );
    $response = @file_get_contents($url, false, $context); 
    return json_decode($response);
}

Leave a Comment

© 2014 Powered By