Daniel
Daniel

Reputation: 689

Facebook JS api: Not receiving user email despite asking for permissions

For my facebook canvas app I've been trying to access the users email. Even though I ask for it, it still doesn't show up (birthday, which is also an additional permission, does show up). (in the addscope function)

<html>
<head></head>
<body>
<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
  FB.init({
    appId      : '--------------', // App ID
    channelUrl : 'http://www.---------.com/channel.html', // Channel File
    status     : true, // check login status
    cookie     : true, // enable cookies to allow the server to access the session
    xfbml      : true  // parse XFBML
  });


    FB.Event.subscribe('auth.statusChange', function(response) {
        if(response.status === 'connected')
            {
                console.log('Hello there!');
                addScope();

            }
        else if(response.status === 'not_authorized')
            {
               console.log('not authorized');
                addScope();

            }
        else
            { 
                console.log('not logged in');
                addScope();
            }
    });



  FB.Event.subscribe('auth.authResponseChange', function(response) {
    // Here we specify what we do with the response anytime this event occurs. 
    if (response.status === 'connected') {
      // ...
    } else if (response.status === 'not_authorized') {
      // ...

    } else {
      //...
    }
  });
  };

  // Load the SDK asynchronously
  (function(d){
   var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
   if (d.getElementById(id)) {return;}
   js = d.createElement('script'); js.id = id; js.async = true;
   js.src = "//connect.facebook.net/en_US/all.js";
   ref.parentNode.insertBefore(js, ref);
  }(document));


  function addScope()
  {
      FB.login(function(response) {
   if (response.authResponse) {
     console.log('Welcome!  Fetching your information.... ');
     FB.api('/me', function(response) {
       console.log('In addscope function, ' + response.email + response.birthday);
     });
   } else {
     console.log('User cancelled login or did not fully authorize.');
   }
 },{scope: 'email,user_birthday'});

  }
</script>

</body>
</html>

Upvotes: 0

Views: 690

Answers (3)

sahithya
sahithya

Reputation: 71

An effective way to see if the right permissions are being set through the code is to use the following code (change the fields that you want to check for in the query part):

FB.api({ method: 'fql.query', query: 'SELECT user_status,friends_status,user_photos,friends_photos,user_location,friends_location FROM permissions WHERE uid=me()' }, function(resp) {   
for(var key in resp[0]) {   
    if(resp[0][key] === "1")   
        console.log(key+' is granted');   
    else   
        console.log(key+' is not granted');   
}   
});

It is possible the user might not have given the permissions to access email id.

Upvotes: 2

Rahul
Rahul

Reputation: 1179

remove this :

FB.Event.subscribe('auth.authResponseChange', function(response) {
// Here we specify what we do with the response anytime this event occurs. 
if (response.status === 'connected') {
  // ...
} else if (response.status === 'not_authorized') {
  // ...

} else {
  //...
}
});
};

and try again

Upvotes: 0

Sean Piwxzc
Sean Piwxzc

Reputation: 13

As far as I can see, and providing there are no errors being thrown elsewhere and that the "addScope" method is being called, your code is correct. Just make sure that there actually is a value to return (that the user has a birthday and email, otherwise you will not get it back. as stated here: https://developers.facebook.com/docs/reference/api/user/ )

I am using the FB api and have had no problem getting email and other fields while using the User access token.

Upvotes: 1

Related Questions