0
私はで新しいアプリケーションを作成しました。r_basicprofileとr_emailaddressフラグをactiveに設定しました。Linkedin(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;
}
}
そして、次のログインコード:あなたのコードのこのセクションで見れば
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)
こんにちはジャスティン、ありがとう。スコープを 'r_basicprofile + r_emailaddress'に変更した後、それは機能しました。 – Reuven