4

I have a website with the Facebook login implementation. My app has the permission for the user's email. Even though the user logs into my app using Facebook I can only retrieve his/her name or basic info. Not the email. So, here's the code I literally copied from the Facebook Developers Website:

 window.fbAsyncInit = function() {
FB.init({
appId      : 'xxxxxxxxxxxxxxx', // App ID
channelUrl : '//domain.org/channel.html', // Channel File
status     : true, // check login status
cookie     : true, // enable cookies to allow the server to access the session
xfbml      : true  // parse XFBML
});


FB.Event.subscribe('auth.authResponseChange', function(response) {

if (response.status === 'connected') {

  testAPI();
} else if (response.status === 'not_authorized') {

  FB.login();
} else {

  FB.login();
}
});
};

// Load the SDK asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));


function testAPI() {
console.log('Welcome!  Fetching your information.... ');
FB.api('/me', function(response) {
  console.log('Good to see you, ' + response.email + '.');
});
}
</script>
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Federico Capello
  • 1,088
  • 3
  • 12
  • 21

7 Answers7

25

You now have to specify the fields you want to get, it´s called "Declarative Fields":

FB.api('/me', {fields: 'name,email'}, function(response) {
  console.log('Good to see you, ' + response.email + '.');
});

See the changelog for more information: https://developers.facebook.com/docs/apps/changelog#v2_4

andyrandy
  • 72,880
  • 8
  • 113
  • 130
4

you probably don't have email permission even though you think you do.

Try,

FB.login(function(){
    FB.api('me',
    function(response){
            console.log(response.email);
    });
},{'scope':'email'});
sakibmoon
  • 2,026
  • 3
  • 22
  • 32
alexl
  • 1,842
  • 1
  • 14
  • 15
  • 1
    I had the permission, the problem was regarding my account. I created another Facebook account and the login could display the email. – Federico Capello Aug 12 '13 at 17:12
2

The user probably had a non-verified email on Facebook. What you could do is to ask them to verify their email on the Facebook settings (Facebook will send them an email with a link to click on) and try again.

Gabriel Saca
  • 150
  • 6
1

You can check this https://developers.facebook.com/bugs/298946933534016/ to see that it's not a bug actually, it's just possible and correct that the user has no email registered in facebook due to several reasons. For example have created the account from mobile phone or haven't validated his or her email...

So it's a known issue. The best workaround maybe could be to ask for the email in a form if we cannot get it from fb.

The problem for me is how to reproduce the empty email field to test the solution provided

Editing.. This is what they say in the facebook developers site:

...The documentation for the 'email' field of the 'user' object ( https://developers.facebook.com/docs/reference/api/user/ ) clarifies the expected behaviour here, which is: "this field will not be returned if no valid email address is available".

There are a number of circumstances in which you may think a user should have an email address returned but they will not. For privacy and security reasons it is not possible to elaborate on the precise reason any specific user's email address will not be returned so please do not ask. Some possible reasons:

No Email address on account; No confirmed email address on account; No verified email address on account; User entered a security checkpoint which required them to reconfirm their email address and they have not yet done so; Users's email address is unreachable.

You also need the 'email' extended permission, even for users who have a valid, confirmed, reachable email address on file...

Hugo
  • 1,662
  • 18
  • 35
0

If the problem persists in the particular email account. Solution is: you have to ask the people to login with their facebook email id The issue with me was undefined email when i connect with my personal account Then tried lot of the things finally logged in with my facebook email id .It worked

The facebook account which has the username defined that makes the problem to log into your app

Shuu
  • 1
0

There is problem in email. If the user email is not verified then it will show an undefined, If the email is well verified then it will show the logged in email. Please try to login and try to print the name, I have solved my problem in this way.. Check this link which is mentioned by Hugo. Its really helpful. https://developers.facebook.com/bugs/298946933534016/

0
    <html>
    <head></head>
    <div id="fb-root"></div>
    <script>
    window.fbAsyncInit = function() {
    FB.init({
        appId      : 'xxxxxxxxxxxxx',
        status     : true,
        cookie     : true, 
        xfbml      : true  
    });

    FB.Event.subscribe('auth.authResponseChange', function(response) {

        if (response.status === 'connected') {
        testAPI();
        } else if (response.status === 'not_authorized') {
        FB.login();
        } else {
        FB.login();
        }
    });
    };

    (function(d){
    var js, id = 'facebook-jssdk', ref =     d.getElementsByTagName('script')[0];
        if (d.getElementById(id)) {return;}
    js = d.createElement('script'); js.id = id; js.async = true;
    js.src = "//connect.facebook.net/en_US/all.js";
    ref.parentNode.insertBefore(js, ref);
    }(document));

    function testAPI() {
        FB.api('/me?fields=name,email', function(response) {
        console.log(response.name + '---'+response.email);
        });
    }
    </script>

    <fb:login-button show-faces="true" width="200" max-rows="1"     scope="public_profile,email"></fb:login-button>

    </body>
    </html>

ref:Getting user email undefined using facebook api

May Noppadol
  • 638
  • 8
  • 9
  • Welcome to Stack Overflow! Please don't answer just with source code. Try to provide a nice description about how your solution works. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Thanks – sɐunıɔןɐqɐp Sep 24 '18 at 07:45