Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to Access User's email. Getting "Undefined"

I have a website with the Facebook login implementation. My app has the permission for the user's email. Even though the user logs into my app using Facebook I can only retrieve his/her name or basic info. Not the email. So, here's the code I literally copied from the Facebook Developers Website:

 window.fbAsyncInit = function() {
FB.init({
appId      : 'xxxxxxxxxxxxxxx', // App ID
channelUrl : '//domain.org/channel.html', // Channel File
status     : true, // check login status
cookie     : true, // enable cookies to allow the server to access the session
xfbml      : true  // parse XFBML
});


FB.Event.subscribe('auth.authResponseChange', function(response) {

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

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

  FB.login();
} else {

  FB.login();
}
});
};

// Load the SDK asynchronously
(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() {
console.log('Welcome!  Fetching your information.... ');
FB.api('/me', function(response) {
  console.log('Good to see you, ' + response.email + '.');
});
}
</script>
like image 951
Federico Capello Avatar asked Nov 28 '22 05:11

Federico Capello


1 Answers

You now have to specify the fields you want to get, it´s called "Declarative Fields":

FB.api('/me', {fields: 'name,email'}, function(response) {
  console.log('Good to see you, ' + response.email + '.');
});

See the changelog for more information: https://developers.facebook.com/docs/apps/changelog#v2_4

like image 155
andyrandy Avatar answered Dec 30 '22 07:12

andyrandy