Lorenzo Tosone
Lorenzo Tosone

Reputation: 415

FB API GRAPH: why I can't catch the user email?

I'm trying to retreive the email of a user logging into my web app; here's my code:

  FB.api('/me', function(response) {
    console.log(response.name + ', ' + response.email);
  });

the user name is correct, but email results as

undefined

Where am I going wrong?

Upvotes: 0

Views: 2293

Answers (1)

andyrandy
andyrandy

Reputation: 73984

https://developers.facebook.com/docs/reference/javascript/FB.login/v3.2

Ask for the email permission in the login process:

FB.login((response) => {
  // handle the response
}, {scope: 'email'});

Also, you need to ask for the fields you want to get:

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

Make sure the user even has an Email, it´s not required. And make sure you actually get asked for the email permission in the login popup.

Side Note: I would just use console.log(response), so you can see the whole object instead of some undefined values.

Upvotes: 4

Related Questions