Ajitej Kaushik
Ajitej Kaushik

Reputation: 944

Unable to retrieve email through Facebook API

I am using the Facebook API for login in the CodeIgniter framework; I am able to retrieve the username and id but am unable to get the email. I have read about the permissions, yet no lead in this regard.

My controller:

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

The view file:

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

Upvotes: 0

Views: 96

Answers (1)

andyrandy
andyrandy

Reputation: 73984

"Declarative Fields" is what you need, see the changelog: https://developers.facebook.com/docs/apps/changelog#v2_4

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

Upvotes: 1

Related Questions