0

I was using v2.2 for a long time, all worked well. However I need to upgrade to v2.8 for some reason and the retrieval of email is not working anymore. Can anybody tell me how the code should look like?

Working v2.2 code that retrieved the email address:

FB.api('/me', function(response) {
  var fn = ('first_name' in response) ? response.first_name : "null";
  var ln = ('last_name' in response) ? response.last_name : "null";
  var fid = ('id' in response) ? response.id : "null";
  var mail = ('email' in response) ? response.email : "null";
  ...
});

Initialization is done via this:

FB.init({
  appId      : 'myid',
  cookie     : true,
  xfbml      : true,
  version    : 'v2.8'
});

And the login (which is also done before the API call of course) looks like this:

FB.login(function(response) {
  if (response.authResponse) {
    processLogin(response);
  } else {
    // user clicked cancel
  }
}, {scope: 'public_profile,email'});

Solution
Here is the working v2.8 solution by using "declarative fields" in the API call:

FB.api('/me', {fields: 'first_name,last_name,email,id'}, function(response) {
  var fn = ('first_name' in response) ? response.first_name : "null";
  var ln = ('last_name' in response) ? response.last_name : "null";
  var fid = ('id' in response) ? response.id : "null";
  var mail = ('email' in response) ? response.email : "null";
  ...
});
basZero
  • 4,129
  • 9
  • 51
  • 89
  • Ok, it's fair to link to the duplicate. However i don't understand the minus rating as my question was regarding v2.8 and the other was regarding v2.4... – basZero Nov 01 '16 at 15:37

1 Answers1

2
FB.api('/me', {fields: 'name,email'}, (response) => {
    console.log(response);
});

It is called "Declarative Fields" and came with v2.4 of the Graph API: https://developers.facebook.com/docs/apps/changelog#v2_4

andyrandy
  • 72,880
  • 8
  • 113
  • 130