简体   繁体   中英

Get LinkedIn profile from OAuth REST API with PHP

I used this site: http://blog.thewebcafes.com/post/1502524959/php-step-by-step-oauth-for-dummies-based-on

to create the following php page:

<?php
  if(!isset($_SESSION)){session_start();}

  define('LINKEDIN_KEY', '**************');
  define('LINKEDIN_SECRET', '****************');

  //Prepare The Links
  $links = array(
    'request_token'=>'https://api.linkedin.com/uas/oauth/requestToken',
    'authorize'=>'https://www.linkedin.com/uas/oauth/authorize',
    'access_token'=>'https://api.linkedin.com/uas/oauth/accessToken'
  );

  //Validate Parameters Passed Back by LinkedIn
  if(isset($_GET['oauth_token']) && isset($_GET['oauth_verifier']) && $_GET['oauth_token']==$_SESSION['linkedin_oauth_token']) {
    //we have user permission, let's get a token
    getAccessToken();
    getProfile();
  }else {
    //we don't have an authed user yet, let's ask for permission
    authUser();
  }

  function urlencode_oauth($str) {
    return str_replace('+',' ',str_replace('%7E','~',rawurlencode($str)));
  }

  function getProfile() {
    global $links;
    //Prepare OAuth Parameters
    $params = array(
      'oauth_consumer_key'=>LINKEDIN_KEY,
      'oauth_nonce'=>sha1(microtime()),
      'oauth_signature_method'=>'HMAC-SHA1',
      'oauth_timestamp'=>time(),
      'oauth_token'=>$_SESSION['linkedin_access_token'],
      'oauth_version'=>'1.0'
    );

    //Prepare The Base String
    // sort parameters according to ascending order of key
    ksort($params);

    // prepare URL-encoded query string
    $q = array();
    foreach ($params as $key=>$value) {
      $q[] = urlencode_oauth($key).'='.urlencode_oauth($value);
    }
    $q = implode('&',$q);

    // generate the base string for signature
    $parts = array(
      'GET',
      urlencode_oauth('https://api.linkedin.com/v1/people/~:(id,num-connections,picture-url)?format=json'),
      urlencode_oauth($q)
    );
    $base_string = implode('&',$parts);

    //Get The Signature
    $key = urlencode_oauth(LINKEDIN_SECRET) . '&' . urlencode_oauth($_SESSION['linkedin_access_token_secret']);
    $signature = base64_encode(hash_hmac('sha1',$base_string,$key,true));

    //Put the Signature into Parameters and Prepare Authorization Header
    $params['oauth_signature'] = $signature;
    $str = array();
    foreach ($params as $key=>$value) {
      $str[] = $key . '="'.urlencode_oauth($value).'"';
    }
    $str = implode(', ',$str);
    $headers = array(
      'GET /v1/people/~ HTTP/1.1',
      'Host: api.linkedin.com',
      'Authorization: OAuth '.$str,
      'Connection: Keep-Alive',
      'Content-Type: application/json',
      'x-li-format: json'
    );

    //Send GET Request To LinkedIn
    $fp = fsockopen("ssl://api.linkedin.com",443,$errno,$errstr,30);
    if(!$fp) { echo 'Unable to connect to LinkedIn'; exit(); }
    $out = implode("\r\n",$headers)."\r\n\r\n".$xml . "\r\n\r\n";
    fputs($fp,$out);

    // getting LinkedIn server response
    $res = '';
    while (!feof($fp)) $res .= fgets($fp,4096);
    fclose($fp);
    echo 'RESPONSE: ' . $res;
    //Make sure the GET is successful
    $parts = explode("\n\n",str_replace("\r","",$res));
    $headers = explode("\n",$parts[0]);
    if ($headers[0] != 'HTTP/1.1 201 Created') { echo 'Failed'; }
  }

  function getAccessToken() {
    global $links;
    //Prepare OAuth Parameters
    $params = array(
      'oauth_consumer_key'=>LINKEDIN_KEY,
      'oauth_nonce'=>sha1(microtime()),
      'oauth_signature_method'=>'HMAC-SHA1',
      'oauth_timestamp'=>time(),
      'oauth_token'=>$_GET['oauth_token'],
      'oauth_verifier'=>$_GET['oauth_verifier'],
      'oauth_version'=>'1.0'
    );

    //Prepare The Base String
    // sort parameters according to ascending order of key
    ksort($params);

    // prepare URL-encoded query string
    $q = array();
    foreach ($params as $key=>$value) {
      $q[] = urlencode_oauth($key).'='.urlencode_oauth($value);
    }
    $q = implode('&',$q);

    // generate the base string for signature
    $parts = array(
      'POST',
      urlencode_oauth($links['access_token']),
      urlencode_oauth($q)
    );
    $base_string = implode('&',$parts);

    //Get The Signature
    $key = urlencode_oauth(LINKEDIN_SECRET) . '&' . urlencode_oauth($_SESSION['linkedin_oauth_token_secret']);
    $signature = base64_encode(hash_hmac('sha1',$base_string,$key,true));

    //Put the Signature into Parameters and Prepare Authorization Header
    $params['oauth_signature'] = $signature;
    $str = array();
    foreach ($params as $key=>$value) {
      $str[] = $key . '="'.urlencode_oauth($value).'"';
    }
    $str = implode(', ',$str);
    $headers = array(
      'POST /uas/oauth/accessToken HTTP/1.1',
      'Host: api.linkedin.com',
      'Authorization: OAuth '.$str,
      'Content-Type: text/xml;charset=UTF-8',
      'Content-Length: 0',
      'Connection: close'
    );

    //Send POST Request To LinkedIn
    $fp = fsockopen("ssl://api.linkedin.com",443,$errno,$errstr,30);
    if (!$fp) { echo 'Unable to connect to LinkedIn'; exit(); }
    $out = implode("\r\n",$headers) . "\r\n\r\n";
    fputs($fp,$out);

    // getting LinkedIn server response
    $res = '';
    while (!feof($fp)) $res .= fgets($fp,4096);
    fclose($fp);

    //Parse the Response to Get The Access Token and Secret
    $parts = explode("\n\n",str_replace("\r",'',$res));
    $res_headers = explode("\n",$parts[0]);
    if ($res_headers[0] != 'HTTP/1.1 200 OK') {
      echo 'Error getting access token and secret.'; exit();
    }
    parse_str($parts[1],$data);
    if (empty($data['oauth_token'])) {
      echo 'Failed to get LinkedIn access token.'; exit();
    }

    //Save the Request Token and Secret
    $_SESSION['linkedin_access_token'] = $data['oauth_token'];
    $_SESSION['linkedin_access_token_secret'] = $data['oauth_token_secret'];

    // unset the Request Token (not needed anymore)
    unset($_SESSION['linkedin_oauth_token']);
    unset($_SESSION['linkedin_oauth_token_secret']);
  }

  function authUser() {
    global $links;
    //Prepare OAuth Parameters
    $params = array(
      'oauth_callback'=>"https://sxsw.ampthealth.com/linkedin_callback.php",
      'oauth_consumer_key'=>LINKEDIN_KEY,
      'oauth_nonce'=>sha1(microtime()),
      'oauth_signature_method'=>'HMAC-SHA1',
      'oauth_timestamp'=>time(),
      'oauth_version'=>'1.0'
    );

    //Prepare The Base String
    // sort parameters according to ascending order of key
    ksort($params);

    // prepare URL-encoded query string
    $q = array();
    foreach ($params as $key=>$value) {
      $q[] = urlencode_oauth($key).'='.urlencode_oauth($value);
    }
    $q = implode('&',$q);

    // generate the base string for signature
    $parts = array(
      'POST',
      urlencode_oauth($links['request_token']),
      urlencode_oauth($q)
    );
    $base_string = implode('&',$parts);

    //Get The Signature
    $key = urlencode_oauth(LINKEDIN_SECRET) . '&';
    $signature = base64_encode(hash_hmac('sha1',$base_string,$key,true));

    //Put the Signature into Parameters and Prepare Authorization Header
    $params['oauth_signature'] = $signature;
    $str = array();
    foreach ($params as $key=>$value) {
      $str[] = $key . '="'.urlencode_oauth($value).'"';
    }
    $str = implode(', ',$str);
    $headers = array(
      'POST /uas/oauth/requestToken HTTP/1.1',
      'Host: api.linkedin.com',
      'Authorization: OAuth '.$str,
      'Content-Type: text/xml;charset=UTF-8',
      'Content-Length: 0',
      'Connection: close'
    );

    //Send POST Request To LinkedIn
    $fp = fsockopen("ssl://api.linkedin.com",443,$errno,$errstr,30);
    if (!$fp) { echo 'Unable to connect to LinkedIn'; exit(); }
    $out = implode("\r\n",$headers) . "\r\n\r\n";
    fputs($fp,$out);

    // getting LinkedIn server response
    $res = '';
    while (!feof($fp)) $res .= fgets($fp,4096);
    fclose($fp);

    //Parse the Response to Get OAuth Token and Secret
    $parts = explode("\n\n",str_replace("\r",'',$res));
    $res_headers = explode("\n",$parts[0]);
    if ($res_headers[0] != 'HTTP/1.1 200 OK') {
      echo 'Error getting OAuth token and secret.'; exit();
    }
    parse_str($parts[1],$data);
    if (empty($data['oauth_token'])) {
      echo 'Failed to get LinkedIn request token.'; exit();
    }

    //Save the Request Token and Secret
    $_SESSION['linkedin_oauth_token'] = $data['oauth_token'];
    $_SESSION['linkedin_oauth_token_secret'] = $data['oauth_token_secret'];

    //Redirect your user to LinkedIn with the request token
    header('Location: '.$links['authorize'].'?oauth_token='.urlencode($data['oauth_token']));
    exit();
  }
?>

I am able to get the user to authorize and request the access token, but when it comes to getting the basic profile, I'm getting an error: 401 Unauthorized Server. I must have something wrong in my getProfile function, but I'm a bit lost as to what I'm missing.

You have to follow the steps in the linkedin documentation here .

In particular read the Application is approved point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM