-1

I'm integrating FB login on my website and for that I need the email of the user. Here's part of the code

$fb = new Facebook\Facebook([
  'app_id' => 'app-id',
  'app_secret' => 'secret',
  'default_graph_version' => 'v2.8',
]);

$helper = $fb->getJavaScriptHelper();

try {
  $accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
  // When Graph returns an error
  echo 'Graph returned an error: ' . $e->getMessage();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  // When validation fails or other local issues
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
}

if (isset($accessToken)) {
  $fb->setDefaultAccessToken($accessToken);

  try {
    $requestProfile = $fb->get("/me?fields=name,email");
    $profile = $requestProfile->getGraphNode()->asArray();
  } catch(Facebook\Exceptions\FacebookResponseException $e) {
    // When Graph returns an error
    echo 'Graph returned an error: ' . $e->getMessage();
  } catch(Facebook\Exceptions\FacebookSDKException $e) {
    // When validation fails or other local issues
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
  }

  print_r($profile);
}

What I'm getting back is name and id. Even the popup that requests users to grant permissions to the app doesnot ask for the email. And understandably it doesn't return it as well.

I've seen this and many other questions but none of the solutions have worked for me. What am I doing wrong?

Whip
  • 1,891
  • 22
  • 43
  • _"Even the popup that requests users to grant permissions to the app doesnot ask for the email"_ - that means _you_ did not ask for the permission in the part of your code where you trigger login. – CBroe Dec 05 '16 at 09:29
  • ahh..you might be on to something. Let me investigate the javascript – Whip Dec 05 '16 at 12:41

2 Answers2

0

You need to request permission to obtain the user email.

Please review https://developers.facebook.com/docs/php/howto/example_facebook_login to see how to request it (e.g.: permissions = ['email', 'user_likes', 'user_location', 'user_friends', 'user_birthday', 'user_hometown'];)

Refer to https://developers.facebook.com/docs/facebook-login/permissions for the permission reference and https://github.com/thephpleague/oauth2-facebook for more example.

Dominic Harvey
  • 73
  • 1
  • 2
  • 8
  • See https://github.com/thephpleague/oauth2-facebook for very good hits... – Dominic Harvey Dec 05 '16 at 00:52
  • My code is a bit different from the example. I'm not using the same helper. I don't get what's wrong with `$fb->get("/me?fields=name,email")` as it is requesting email but not working – Whip Dec 05 '16 at 04:06
0

Thanks to CBroe who pointed this out in the comments above, the permissions were supposed to be set in the javascript, not the php. It's because the php code is run when the authorization is complete and you have received all information you were supposed to.

To fix it, I changed

FB.login(function(response){
      // take action based on response
});

To

FB.login(function(response){
      // take action based on response
}, {scope: 'public_profile, email'});

And it works now. Thanks

Whip
  • 1,891
  • 22
  • 43