Getting user email undefined using facebook api
Asked Answered
C

2

0

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?

Cloison answered 11/8, 2015 at 10:14 Comment(12)
check for the permissionsDeathbed
you are talking about FB.api('/me', function(response) { console.log(response.name + '---'+response.email); },{'scope':'email'}); ? it too doesn't work. same output.Cloison
are you able to login to fb through your app?Deathbed
yes. and I get user's full name, but not emailCloison
can you add the response result to your question?Deathbed
you don't need to provide scope:email while login, Facebook by default provided those permission.Deathbed
what version of you app and your api?Deathbed
console.log(response) gives output: Object {name: "user's full name", id: "xxxxxxxxxx"}Cloison
Let us continue this discussion in chat.Cloison
Maybe the user is registered with his mobile number instead with email address. I had the same problem in my code.Mare
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?Cloison
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 developers.facebook.com/docs/apps/changelog#v2_4_changes, “Declarative Fields”Sumy
D
9

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);
    });
Deathbed answered 11/8, 2015 at 11:4 Comment(2)
great!! can you tell me, how to get all informations possible to get?Cloison
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.Deathbed
P
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'}
                        );
            }
Philter answered 13/9, 2020 at 14:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.