Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook oauth retrieve user Email

I am using Facebook Javascript SDK to get oauth token and retrieve all the data from FB:

 function login() {     FB.api('/me', function (response) {                     alert('You have successfully logged in, ' + response.name + ", " + response.email + "!");      }); 

I retrieve all basic information (full name, work, school, ID..) but not EMAIL (response.email == undefined) !!

I know it's possible to access email within basic information (not asking for 'email' permissions), how do I do that?

like image 670
Stewie Griffin Avatar asked Apr 18 '11 18:04

Stewie Griffin


People also ask

How do I get my Facebook API user ID?

The simplest way to get a copy of the User Profile object is to access the /me endpoint: FB. api('/me', function(response) { }); This will this will return the users name and an ID by default.

Does Facebook login use OAuth?

OAuth is also used when giving third-party apps access to accounts like your Twitter, Facebook, Google, or Microsoft accounts. It allows these third-party apps access to parts of your account. However, they never get your account password.


1 Answers

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 */ 
like image 160
Balakrishnan Avatar answered Oct 12 '22 09:10

Balakrishnan