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";
...
});