2014-12-01 58 views
0

這是我目前的API查詢:如何檢索整個用戶配置文件(LinkedIn API)

https://api.linkedin.com/v1/people/~:(firstName,lastName) 

然而,這僅返回第一個和最後一個名字。

我的範圍是:

r_fullprofile r_contactinfo r_emailaddress 

如何檢索整個用戶的個人資料?

這裏是我當前的代碼:

 <?php 
     // Change these 
     define('API_KEY',  'xxx'           ); 
     define('API_SECRET', 'xxx'          ); 
     define('REDIRECT_URI', 'xxx'); 
     define('SCOPE',  'r_fullprofile r_contactinfo r_emailaddress'); 

     // 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(); 
      } 
     } 

     // Congratulations! You have a valid token. Now fetch your profile 
     $user = fetch('GET', '/v1/people/~:(firstName,lastName)'); 
     print_r($user); 
     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 = '') { 
      print $_SESSION['access_token']; 

      $opts = array(
       'http'=>array(
        'method' => $method, 
        'header' => "Authorization: Bearer " . $_SESSION['access_token'] . "\r\n" . "x-li-format: json\r\n" 
       ) 
      ); 

      // Need to use HTTPS 
      $url = 'https://api.linkedin.com' . $resource; 

      // Append query parameters (if there are any) 
      if (count($params)) { $url .= '?' . http_build_query($params); } 

      // Tell streams to make a (GET, POST, PUT, or DELETE) request 
      // And use OAuth 2 access token as Authorization 
      $context = stream_context_create($opts); 

      // Hocus Pocus 
      $response = file_get_contents($url, false, $context); 

      // Native PHP object, please 
      return json_decode($response); 
     } 
+0

如果你想從LinkedIn r_fullprofile得到的,你必須要與LinkedIn的合作伙伴。 – 2017-05-20 10:32:01

回答

0

從r_fullprofile到r_basicprofile更換範圍參數。這將解決你的問題。因爲它對我有用。

1
https://api.linkedin.com/v1/people/~:(id,first-name,last-name,headline,picture-url,industry,positions,educations,last-modified-timestamp,proposal-comments,associations,interests,publications,patents,languages,skills,certifications,courses,volunteer,three-current-positions,three-past-positions,num-recommenders,recommendations-received,following,job-bookmarks,suggestions,date-of-birth,member-url-resources,related-profile-views,honors-awards,location)?format=json 

實際上您不必指定每個主要資源屬性的成員。

所以,人的資源看起來是這樣的:

Persons->id      = string 
Persons->first-name    = string 
Persons->last-name    = string 
Persons->headline     = string 
Persons->picture-url    = string 
Persons->industry     = array(child attributes) 
Persons->positions    = array(child attributes) 
Persons->educations    = array(child attributes) 
Persons->last-modified-timestamp = array(child attributes) 
Persons->proposal-comments  = array(child attributes) 
Persons->associations    = array(child attributes) 
Persons->interests    = array(child attributes) 
Persons->publications    = array(child attributes) 
Persons->patents     = array(child attributes) 
Persons->languages    = array(child attributes) 
Persons->skills     = array(child attributes) 
Persons->certifications   = array(child attributes) 
Persons->courses     = array(child attributes) 
Persons->volunteer    = array(child attributes) 
Persons->three-current-positions = array(child attributes) 
Persons->three-past-positions  = array(child attributes) 
Persons->num-recommenders   = array(child attributes) 
Persons->recommendations-received = array(child attributes) 
Persons->following    = array(child attributes) 
Persons->job-bookmarks   = array(child attributes) 
Persons->suggestions    = array(child attributes) 
Persons->date-of-birth   = array(child attributes) 
Persons->member-url-resources  = array(child attributes) 
Persons->related-profile-views = array(child attributes) 
Persons->honors-awards   = array(child attributes) 
Persons->location     = array(child attributes) 
相關問題