Getting user email undefined using facebook api

6.1k views Asked by At

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?

2

There are 2 answers

2
Toretto On BEST ANSWER

I have tried you code with my test app and I did get the same issue. To fix that issue I have tried this and I am able to get email and full name as well.

Replace with this -

FB.api('/me?fields=name,email', function(response) {
      console.log(response.name + '---'+response.email);
    });
0
ship shuk - www.shipshuk.com On

For the full login flow I have used the following code line. Just added prior steps (FB.login -> FB.api) to the previous comment.

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