2016-08-10 2 views
0

에서 새 응용 프로그램을 만들었습니다. r_basicprofiler_emailaddress 플래그가 활성으로 설정되었습니다.링크드 인 (PHP)

사용자 프로필 데이터를 가져 오기 위해 다음 PHP 라이브러리 (CodeIgniter 프레임 워크 사용)를 사용하고 있습니다. 당신이 결과에서 볼 수 있듯이, 나는 어떤 이유로 사용자의 이메일 주소를 얻을 수 없다.

누군가 전자 메일 주소를 얻는 방법을 알고 있습니까?

감사합니다.

class linkedin 
{ 
    private $client_id = "123456789"; 
    private $client_secret = "123456789"; 

    public function in_redirect_url($redirect_url){ 
     $state = $this->in_genState(); 
     return "https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=$this->client_id&state=$state&scope=r_basicprofile&redirect_uri=$redirect_url"; 
    } 

    public function in_genState() 
    { 
     return "123456789"; 
    } 

    public function in_exchange_code_token($code) 
    { 
     // replace code with auth token 
     $url = 'https://www.linkedin.com/oauth/v2/accessToken'; 

     $headers = [ 
      'Content-type: application/x-www-form-urlencoded', 
      'Host: www.linkedin.com', 
     ]; 

     $fields = [ 
      'grant_type' => 'authorization_code', 
      'code' => $code, 
      'redirect_uri' => base_url('login/linkedin'), 
      'client_id' => $this->client_id, 
      'client_secret' => $this->client_secret, 
     ]; 

     //url-ify the data for the POST 
     $fields_string = ''; 
     foreach ($fields as $key => $value) { 
      $fields_string .= $key.'='.$value.'&'; 
     } 
     rtrim($fields_string, '&'); 

     // init curl handle 
     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL, $url); 
     curl_setopt($ch, CURLOPT_POST, true); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); 
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

     // execute! 
     $response = curl_exec($ch); 
     // close the connection, release resources used 
     curl_close($ch); 

     // do anything you want with your response 
     $response = json_decode($response); 

     return $response; 
    } 

    public function in_get_user_data($auth_token) 
    { 
     // replace code with auth token 
     $url = 'https://api.linkedin.com/v1/people/~:(email-address,id,first-name,last-name,location)'; 

     $headers = [ 
      'Host: api.linkedin.com', 
      'Connection: Keep-Alive', 
     ]; 

     $fields = [ 
      'oauth2_access_token' => $auth_token, 
      'format' => 'json', 
     ]; 

     //url-ify the data 
     $fields_string = ''; 
     foreach ($fields as $key => $value) { 
      $fields_string .= $key.'='.$value.'&'; 
     } 
     rtrim($fields_string, '&'); 

     // init curl handle 
     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL, $url.'?'.$fields_string); 
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

     // execute! 
     $response = curl_exec($ch); 
     // close the connection, release resources used 
     curl_close($ch); 

     // do anything you want with your response 
     $response = json_decode($response); 

     return $response; 
    } 
} 

그리고 다음 로그인 코드 :

public function linkedin() 
{ 
    $code = $this->input->get('code'); 
    $state = $this->input->get('state'); 

    if (!$code) { 
     // redirect to linkedin login 
     $local_url = base_url('/login/linkedin'); 
     header("Location: " . $this->linkedin->in_redirect_url($local_url)); 
     die(); 

     return; 
    } 

    // verify state 
    if ($state != $this->linkedin->in_genState()) { 
     echo 'Invalid request'; 
     die(400); 
    } 

    $response = $this->linkedin->in_exchange_code_token($code); 

    if (property_exists($response, 'access_token')) { 
     echo 'Success !<br/>'; 
     var_dump($response); 
     $access_token = $response->access_token; 
     var_dump($this->linkedin->in_get_user_data($access_token)); 
    } else { 
     echo 'Error !<br/>'; 
     var_dump($response); 
    } 
} 

이 예제 결과입니다

Success ! 

object(stdClass)[74] 
    public 'access_token' => string '*****' (length=179) 
    public 'expires_in' => string '5184000000' (length=10) 

object(stdClass)[75] 
    public 'firstName' => string '***' (length=6) 
    public 'id' => string '***' (length=10) 
    public 'lastName' => string '***' (length=8) 
    public 'location' => 
    object(stdClass)[76] 
     public 'country' => 
     object(stdClass)[77] 
      public 'code' => string 'us' (length=2) 
     public 'name' => string 'United States' (length=6) 

답변

0

당신은 당신의 코드 섹션을 보면 :

public function in_redirect_url($redirect_url){ 
     $state = $this->in_genState(); 
     return "https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=$this->client_id&state=$state&scope=r_basicprofile&redirect_uri=$redirect_url"; 
    } 

귀하의 &scope= p arameter는 r_emailaddress이 아니라 r_basicprofile만을 요청합니다. LinkedIn 응용 프로그램의 기본 권한을 사용하여 응용 프로그램에서 요청한 권한을 제어하려는 경우 인증 흐름에서 범위를 지정할 수 없습니다. 그렇게하면 여기에서 변경되는 내용이 무시되고 범위를 모든 구성원 권한이 포함되지 않은 값으로 재정의합니다.

+0

안녕 저스틴, 정말 고마워요. 범위를'r_basicprofile + r_emailaddress'로 바꾼 후에도 효과가있었습니다. – Reuven

관련 문제