44

I am using Facebook Javascript SDK to get oauth token and retrieve all the data from FB:

 function login() {
    FB.api('/me', function (response) {            
        alert('You have successfully logged in, ' + response.name + ", " + response.email + "!");

    });

I retrieve all basic information (full name, work, school, ID..) but not EMAIL (response.email == undefined) !!

I know it's possible to access email within basic information (not asking for 'email' permissions), how do I do that?

Stewie Griffin
  • 9,257
  • 22
  • 70
  • 97

4 Answers4

82

You need get email explicitly as follows:

                    var url = '/me?fields=name,email';
                    FB.api(url, function (response) {
                        alert(response.name);
                        alert(response.email);
                    });

And to get the email address, email permission is needed. So the code might look like (there are other options like Register button, login button..)

            FB.login(function (response) {
                if (response.session) {
                    var url = '/me?fields=name,email';
                    FB.api(url, function (response) {
                        alert(response.name);
                        alert(response.email);
                    });
                }
                else {
                    alert("User did not login successfully");
                }
            }, { scope: 'email' }); /* perms changed to scope */
Simon B.
  • 2,530
  • 24
  • 30
Balakrishnan
  • 1,073
  • 9
  • 9
8

First make your app live, available to public. Then you have to grant permission to get user 'email' which is not allowed default by the app. After that the login method should be changed as follows.

function checkLoginState() {
    FB.getLoginStatus(function(response) {
        statusChangeCallback(response);
        console.log('Welcome!  Fetching your information.... ');
        var url = '/me?fields=id,name,email';
        FB.api(url, function(response) {
             alert(response.name + " " + response.id + " " +response.email);
        }, {scope: 'email'});
    });
}

If the field id is not needed change the variable url as :

var url = '/me?fields=name,email';
Vihanga Yasith
  • 328
  • 5
  • 18
2

If you can change to use a promise chaining, this worked for me:

fb.login({scope:'email'}).then(function (resp) {
    if (resp.status === 'connected') {
       fb.api('/me', 'get', {fields:'id,email,first_name,gender,...'}).then(function (response) {
          alert('You have successfully logged in, ' + response.name + ", " + response.email + "!");
       });
    }
});
Pat M
  • 5,966
  • 7
  • 24
  • 34
-5

It is impossible, user have to grant your application to access his email information, or in other case, it can be accessible if user allowed his email to viewed by everybody in his privacy settings.