0

I want to get the users email id from facebook i user java script SDK for getting email id

  <script>

(function(d){
    var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "https://connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));

     $(document).ready(function() {
     $('#fb_button').click(function() {
     FB._https = (window.location.protocol == "https:"); // Required because FB Javascript SDK tries to submit https to http
    FB.init({appId:"{app_id}", cookie:true, status:true, xfbml:true, oauth:true , version:'v2.5'});
    FB.login(function(response) {

    if (response.authResponse) {
    console.log('Welcome!  Fetching your information.... ');
    console.log(response); // dump complete info
    access_token = response.authResponse.accessToken; //get access token
    user_id = response.authResponse.userID; //get FB UID


    FB.api('/me', function(response) {
        user_email = response.email; //get user email
        alert(user_email);
  });  
  }
  else{
  alert('no response');
  }   
}, {
    scope: 'email'

    });

     });
});


(function() {
    var e = document.createElement('script');
    e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
    e.async = true;
  //  document.getElementById('fb-root').appendChild(e);
    }());
     </script>

it returns response but undefined value for alert i copy paste the same code in another site with different app_id it returns the email id for same user. i also tried PHP SDK but same result.may be this question is duplicate .but i tried almost all answers of those questions.

Anyone got any idea what's going wrong please?

Thanks in advance

Ashith
  • 309
  • 1
  • 3
  • 17

1 Answers1

3

"Declarative Fields" is what you need, see the changelog: https://developers.facebook.com/docs/apps/changelog#v2_4

For example:

FB.api('/me', {fields: 'name,email'}, function(response) {
    user_email = response.email; //get user email
    alert(user_email);
});
andyrandy
  • 72,880
  • 8
  • 113
  • 130