Search code examples
facebookemailfacebook-javascript-sdkfacebook-apps

how to get email id of Facebook user using javascript sdk


I am using JavaScript API to create my app for Facebook. The problem is, it's returning
email = undefined.

I don't know why? And if I use Facebook login/logout button on my app then the alert shows correct email id of the user but I don't want to do that. What am I missing?

Here is my code:

<p><fb:login-button autologoutlink="true" perms="user_about_me,email"></fb:login-button></p>

<script>
window.fbAsyncInit = function () {
  FB.init({ appId: '250180631699888', status: true, cookie: true,
  xfbml: true
});

  FB.getLoginStatus(function (response) {
    if (response.session) {
      greet();
    }
  });
};
(function () {
  var e = document.createElement('script');
  e.type = 'text/javascript';
  e.src = document.location.protocol +
  '//connect.facebook.net/en_US/all.js';
  e.async = true;
  document.getElementById('fb-root').appendChild(e);
} ());

function greet() {
  FB.api('/me', function (response) {
  alert('Welcome, ' + response.name + "!");
  alert('Your email id is : '+ response.email);
});
}
</script>

Solution

  • According to the latest info on the facebook page you should use 'scope' instead of perms. https://developers.facebook.com/docs/reference/javascript/FB.login/

    If you visit

    https://developers.facebook.com/tools/console/

    and use the fb-api -> user-info example as a starting point, then logout and back in again, it should ask you for email perms and you can see your email being printed. It is done using response.email as you mention in your post.