Email with facebook javascript api

I am trying to get a facebook user by email for my application. But when I request, the email address is returned as undefined. I don’t understand how to deal with this.

Here is my code -

<body>
  <script>
    function statusChangeCallback(response) {
      console.log('statusChangeCallback');
      console.log(response);

      if (response.status === 'connected') {

        testAPI();
      } else if (response.status === 'not_authorized') {

        FB.login(function(response) {}, {
          scope: 'email'
        });
        document.getElementById('status').innerHTML = 'Please log ' +
          'into this app.';
      } else {
        FB.login(function(response) {}, {
          scope: 'email'
        });
        document.getElementById('status').innerHTML = 'Please log ' +
          'into Facebook.';
      }
    }

    function checkLoginState() {
      FB.getLoginStatus(function(response) {
        statusChangeCallback(response);
      });
    }

    window.fbAsyncInit = function() {
      FB.init({
        appId: 'app id',
        cookie: true, // enable cookies to allow the server to access 

        xfbml: true, // parse social plugins on this page
        version: 'v2.1' // use version 2.1
      });

      FB.getLoginStatus(function(response) {
        statusChangeCallback(response);
      });

    };

    (function(d, s, id) {
      var js, fjs = d.getElementsByTagName(s)[0];
      if (d.getElementById(id)) return;
      js = d.createElement(s);
      js.id = id;
      js.src = "//connect.facebook.net/en_US/sdk.js";
      fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));

    function testAPI() {
      console.log('Welcome!  Fetching your information.... ');
      FB.api('/me', function(response) {
        alert(response.name);
        alert(response.email);
        console.log('Successful login for: ' + response.name);
        document.getElementById('status').innerHTML = 'Thanks for logging in, ' + response.name + '!';
      });
    }
  </script>
  <fb:login-button perms="email" autologoutlink="true" onlogin="checkLoginState();">
  </fb:login-button>
  <div id="status">
  </div>
</body>
+4
source share
2 answers

You must use the attribute scopeinstead permsin the login button, so permission cannot be selected. Try

<fb:login-button scope="public_profile,email" autologoutlink="true" onlogin="checkLoginState();">
</fb:login-button>

Cm:

+1
source

@Tobi it really saved my day:

FB.api('/me

FB.api('/me?fields=id,name,email,permissions', function(response) {
        console.log(response.name);
        console.log(response.email);
    });

, ( facebook)

<fb:login-button scope="public_profile,email" onlogin="checkLoginState();">

+8

All Articles