I am using Facebook Javascript SDK to get oauth token and retrieve all the data from FB:
function login() {
FB.api(\'/me\', function (response) {
It is impossible, user have to grant your application to access his email information, or in other case, it can be accessible if user allowed his email to viewed by everybody in his privacy settings.
First make your app live, available to public. Then you have to grant permission to get user 'email' which is not allowed default by the app. After that the login method should be changed as follows.
function checkLoginState() {
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
console.log('Welcome! Fetching your information.... ');
var url = '/me?fields=id,name,email';
FB.api(url, function(response) {
alert(response.name + " " + response.id + " " +response.email);
}, {scope: 'email'});
});
}
If the field id is not needed change the variable url as :
var url = '/me?fields=name,email';
If you can change to use a promise chaining, this worked for me:
fb.login({scope:'email'}).then(function (resp) {
if (resp.status === 'connected') {
fb.api('/me', 'get', {fields:'id,email,first_name,gender,...'}).then(function (response) {
alert('You have successfully logged in, ' + response.name + ", " + response.email + "!");
});
}
});
You need get email explicitly as follows:
var url = '/me?fields=name,email';
FB.api(url, function (response) {
alert(response.name);
alert(response.email);
});
And to get the email address, email permission is needed. So the code might look like (there are other options like Register button, login button..)
FB.login(function (response) {
if (response.session) {
var url = '/me?fields=name,email';
FB.api(url, function (response) {
alert(response.name);
alert(response.email);
});
}
else {
alert("User did not login successfully");
}
}, { scope: 'email' }); /* perms changed to scope */