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?

partho
  • 1,101
  • 2
  • 21
  • 37
  • 2
    check for the permissions – Toretto Aug 11 '15 at 10:24
  • you are talking about FB.api('/me', function(response) { console.log(response.name + '---'+response.email); },{'scope':'email'}); ? it too doesn't work. same output. – partho Aug 11 '15 at 10:26
  • are you able to login to fb through your app? – Toretto Aug 11 '15 at 10:35
  • yes. and I get user's full name, but not email – partho Aug 11 '15 at 10:37
  • can you add the response result to your question? – Toretto Aug 11 '15 at 10:40
  • you don't need to provide `scope:email` while login, Facebook by default provided those permission. – Toretto Aug 11 '15 at 10:42
  • what version of you app and your api? – Toretto Aug 11 '15 at 10:44
  • console.log(response) gives output: Object {name: "user's full name", id: "xxxxxxxxxx"} – partho Aug 11 '15 at 10:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/86687/discussion-between-partho-and-toretto). – partho Aug 11 '15 at 10:47
  • Maybe the user is registered with his mobile number instead with email address. I had the same problem in my code. – mario.van.zadel Aug 11 '15 at 10:56
  • i am the user(tested by 2 accounts), and i have email address, not mobile number, can you tell me, how to get other info except email? – partho Aug 11 '15 at 11:00
  • 1
    This is due to a change API v2.4 introduced (and that has been mentioned here a lot already, in a lot of other questions where people could not be bothered to read the changelog before asking as well) – see https://developers.facebook.com/docs/apps/changelog#v2_4_changes, “Declarative Fields” – CBroe Aug 11 '15 at 12:47

2 Answers2

10

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);
    });
Toretto
  • 4,721
  • 5
  • 27
  • 46
  • great!! can you tell me, how to get all informations possible to get? – partho Aug 11 '15 at 11:08
  • by default you can get the user public profile , user friends and email only for advanced permissions you need to send the app for review to facebook. – Toretto Aug 11 '15 at 11:11
0

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'}
                        );
            }
Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107