无法通过Facebook API检索电子邮件

时间:2015-11-16 08:48:55

标签: php facebook codeigniter

我使用Facebook API登录CodeIgniter框架;我能够检索用户名和ID,但无法收到电子邮件。我已经阅读了关于权限的内容,但在这方面没有领先。

我的控制器:

public function login()
{

        $this->load->library('facebook');

        $user = $this->facebook->getUser();


        if ($user) {
            try {
                $data['user_profile'] = $this->facebook->api('/me');
            } catch (FacebookApiException $e) {
                $user = null;
            }
        }else {
            // Solves first time login issue. (Issue: #10)
            $this->facebook->destroySession();
        }
        if ($user) {
            $data['logout_url'] = site_url('user_module/facebook_logout'); // Logs off application
            // OR 
            // Logs off FB!
            // $data['logout_url'] = $this->facebook->getLogoutUrl();
        } else {
            $data['login_url'] = $this->facebook->getLoginUrl(array(
           // 'redirect_uri' => site_url('user_module/home'),
            'scope' => array("email") // permissions here
            ));
        }

        $this->load->view('user_login',$data);
}  

视图文件:

<?php if (@$user_profile):  // call var_dump($user_profile) to view all data ?>
     <div class="row">
         <div class="col-lg-12 text-center">
             <img class="img-thumbnail" data-src="holder.js/140x140" alt="140x140" src="https://graph.facebook.com/<?=$user_profile['id']?>/picture?type=large" style="width: 140px; height: 140px;">
             <h2><?=$user_profile['name']?></h2>
             <h2><?=$user_profile['id']?></h2>
             <h2><?=$user_profile['email']?></h2>
             <h2><?=$user_profile['user_location']?></h2>
             <a href="<?=$user_profile['link']?>" class="btn btn-lg btn-default btn-block" role="button">View Profile</a>
             <a href="<?= $logout_url ?>" class="btn btn-lg btn-primary btn-block" role="button">Logout</a>
         </div>
     </div>
<?php else: ?>
      <h2 class="form-signin-heading">Login with Facebook</h2>
      <a href="<?= $login_url ?>" class="btn btn-lg btn-success btn-block" role="button">Login</a>
<?php endif; ?>

1 个答案:

答案 0 :(得分:1)

“声明字段”是您所需要的,请参阅更改日志:https://developers.facebook.com/docs/apps/changelog#v2_4

例如:$this->facebook->api('/me?fields=name,email')