Vous obtenez donc les codes nécessaires au paramétrage de votre authentification que vous allez utiliser dans votre script.
Conclusion : en comparaison de l’API Twitter, l’API linkedin semble moins bien documenté. L’authentification est plus contraignante sans doute parce les données y sont peut-être plus sensibles sur cette plate-forme professionnelle mais enfin on parvient finalement à ces fins. La bonne nouvelle c’est que si vous faites les modifications nécessaires pour le partage sur Facebook, les données Open Graph Protocol
seront utilisées à l’identique sur Linkedin
Un script d’authentification et de rapatriement des données de votre compte Linkedin.
<?php // Change these // define('API_KEY', 'YOUR_API_KEY_HERE' ); // define('API_SECRET', 'YOUR_API_SECRET_HERE' ); /* To change with your own credentials */ define('API_KEY', 'msqi8kmf9dy4' ); define('API_SECRET', 'RP71sq5fGWwLMbsN' ); define('REDIRECT_URI', 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME']); define('SCOPE', 'r_fullprofile r_emailaddress rw_nus' ); // You'll probably use a database session_name('linkedin'); session_start(); // OAuth 2 Control Flow if (isset($_GET['error'])) { // LinkedIn returned an error print $_GET['error'] . ': ' . $_GET['error_description']; exit; } elseif (isset($_GET['code'])) { // User authorized your application if ($_SESSION['state'] == $_GET['state']) { // Get token so you can make API calls getAccessToken(); } else { // CSRF attack? Or did you mix up your states? exit; } } else { if ((empty($_SESSION['expires_at'])) || (time() > $_SESSION['expires_at'])) { // Token has expired, clear the state $_SESSION = array(); } if (empty($_SESSION['access_token'])) { // Start authorization process getAuthorizationCode(); } } define ('_SOURCE_LINKEDIN_', '/v1/people/~:(id,first-name,last-name,headline,industry)'); $user = fetch('GET', ''._SOURCE_LINKEDIN_.''); // http://api.linkedin.com/ /* http://developer.linkedin.com/apis */ print (''.$user->id.''); echo ('<br>'); print (''.$user->firstName.''); echo ('<br>'); print (''.$user->lastName.''); echo ('<br>'); print (''.$user->headline.''); echo ('<br>'); /* print (''.$user->picture-url.''); echo ('<br>'); */ print (''.$user->industry.''); echo ('<br>'); // print "Hello $user->firstName $user->lastName."; exit; function getAuthorizationCode() { $params = array('response_type' => 'code', 'client_id' => API_KEY, 'scope' => SCOPE, 'state' => uniqid('', true), // unique long string 'redirect_uri' => REDIRECT_URI, ); // Authentication request $url = 'https://www.linkedin.com/uas/oauth2/authorization?' . http_build_query($params); // Needed to identify request when it returns to us $_SESSION['state'] = $params['state']; // Redirect user to authenticate header("Location: $url"); exit; } function getAccessToken() { $params = array('grant_type' => 'authorization_code', 'client_id' => API_KEY, 'client_secret' => API_SECRET, '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, false, $context); // Native PHP object, please $token = json_decode($response); // Store access token and expiration time $_SESSION['access_token'] = $token->access_token; // guard this! $_SESSION['expires_in'] = $token->expires_in; // relative time (in seconds) $_SESSION['expires_at'] = time() + $_SESSION['expires_in']; // absolute time return true; } function fetch($method, $resource, $body = '') { $params = array('oauth2_access_token' => $_SESSION['access_token'], 'format' => 'json', ); // Need to use HTTPS $url = 'https://api.linkedin.com' . $resource . '?' . http_build_query($params); // print_r($url); // Tell streams to make a (GET, POST, PUT, or DELETE) request $context = stream_context_create( array('http' => array('method' => $method, ) ) ); // Hocus Pocus $response = file_get_contents($url, false, $context); // Native PHP object, please return json_decode($response); } ?> |