how to get email id of Facebook user using javascript sdk

user958414 picture user958414 · Oct 19, 2011 · Viewed 96.6k times · Source

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>

Answer

masakielastic picture masakielastic · Aug 1, 2015
// https://developers.facebook.com/docs/javascript/reference/FB.api/
// v2.4

FB.api('/me', { locale: 'en_US', fields: 'name, email' },
  function(response) {
    console.log(response.email);
  }
);