Unable to Access User's email. Getting “Undefined”

心不动则不痛 提交于 2020-01-11 05:40:11

问题


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>

回答1:


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'});



回答2:


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




回答3:


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.




回答4:


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...




回答5:


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




回答6:


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/




回答7:


    <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



来源:https://stackoverflow.com/questions/18166089/unable-to-access-users-email-getting-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!