【问题标题】:Get user e-mail from Linkedin (PHP)从 Linkedin (PHP) 获取用户电子邮件
【发布时间】:2016-12-16 08:03:48
【问题描述】:

我在 Linkedin 中创建了一个新应用程序,其中 r_basicprofiler_emailaddress 标志设置为活动。

我正在使用以下 PHP 库(带有 CodeIgniter 框架)来获取用户配置文件数据。从结果中可以看到,由于某种原因,我无法获取用户的电子邮件地址。

有人知道如何获取电子邮件地址吗?

谢谢。

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

    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)

【问题讨论】:

    标签: php oauth-2.0 linkedin linkedin-api


    【解决方案1】:

    如果您查看代码的这一部分:

    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";
        }
    

    请注意,您的 &amp;scope= 参数仅请求 r_basicprofile,而不是 r_emailaddress。如果您想依靠您的 LinkedIn 应用程序的默认权限来控制您的应用程序请求什么权限,您不能在您的身份验证流程中指定范围。如果您这样做,它将覆盖它 - 这就是这里发生的事情,并且您将您的范围覆盖为一个不包括您认为的所有成员权限的值。

    【讨论】:

    • 嗨贾斯汀,非常感谢。在我将范围更改为 r_basicprofile+r_emailaddress 后,它起作用了。
    猜你喜欢
    • 2014-06-01
    • 2015-03-28
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    • 2020-06-07
    • 2022-11-15
    • 1970-01-01
    • 2013-09-02
    相关资源
    最近更新 更多