1

I am trying to get user id, name, email. Getting id and name but not email. I have this code:

<html>
<head></head>
body>
<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
  FB.init({
    appId      : 'xxxxxxxxxxxx',
    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', 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>

Output is:

FirstName LastName---undefined

Why email is undefined? I also want to get all informations possible to get. How?

4

2 回答 2

10

我已经用我的测试应用程序尝试过你的代码,但我确实遇到了同样的问题。为了解决这个问题,我已经尝试过了,我也可以得到电子邮件和全名。

用这个替换 -

FB.api('/me?fields=name,email', function(response) {
      console.log(response.name + '---'+response.email);
    });
于 2015-08-11T11:04:04.600 回答
0

对于完整的登录流程,我使用了以下代码行。刚刚在前面的评论中添加了前面的步骤(FB.login -> FB.api)。

function loginTest() 
            {   
                FB.login(function(response)
                            {
                                // handle the response 
                                var i = 0;
                                FB.api('/me?fields=name,email', 
                                        function(response) 
                                        {
                                            console.log(response.name + '---'+response.email);
                                        }
                                      );
                            },
                            {scope: 'email, public_profile'}
                        );
            }

于 2020-09-13T14:35:16.310 回答