doubinei1457 2016-08-10 05:28
浏览 56
已采纳

来自Linkedin(PHP)的用户邮件

I created a new application in Linkedin with r_basicprofile and r_emailaddress flags set to active.

I'm using the following PHP library (with CodeIgniter framework) to get user profile data. As you can see on the results, for some reason I can't get the e-mail address of user.

Someone know how to get the e-mail address ?

Thank you.

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

And the following login code:

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

This is an example result:

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)
  • 写回答

1条回答 默认 最新

  • douguio0185 2016-08-15 17:35
    关注

    If you look in this section of your code:

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

    Note that your &scope= parameter only requests r_basicprofile, and not r_emailaddress. If you want to rely on your LinkedIn application's default permissions to control what permission your app requests, you cannot specify a scope in your auth flow. If you do, it will override it - which is what's happening here, and you're overriding your scope to a value that does not include all of the member permissions that you think.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建