tags:

views:

28

answers:

1

I'm using this code but I can't figure out how to get the email from the user. I know you must get special permission from the user but I don't know how.

I can get the public data from the user but that's not what I even need. The only thing I need is the email.

I guess it's something with this line:

$me = $facebook->api('/me');

I have read the documentation but I still don't know how I can get the email. How do I get the email from the user loggin in on my website with the facebook api?

A: 

You first need to request an extended permission "Email" from the user. You can do this using FB JS SDK by calling FB.login(), or PHP FB SDK by redirecting the browser by calling getLoginUrl().

FB JS SDK: FB.login method

FB.login(function(response) {
  if (response.session) {
    if (response.perms) {
      // user is logged in and granted some permissions.
      // perms is a comma separated list of granted permissions
    } else {
      // user is logged in, but did not grant any permissions
    }
  } else {
    // user is not logged in
  }
}, {perms:'email'});

FB PHP SDK method:

$login_url = $Facebook->getLoginUrl(array(
    'canvas'    => 1,
    'fbconnect' => 0,
    'req_perms' => 'email'
));

echo '<script type="text/javascript">top.location.href = "'.$login_url.'";</script>';
exit();

Once you have the required permission, you can then call this in your PHP:

$me = $facebook->api('/me?fields=email');
jusunlee
I had to change canvas to 0 and fbconnect to 1, or else I wouldn't be sent back to my webpage.I'm only getting a anonymous mail from facebook and that NOT what I want. I want the users real email. You know how I can fix this?
Marwelln
I saw now you could chose your own email or anonymous email while choosing to accept or deny the application. My default option is aonymous tho, how can I change it to default have the users email? StackExchange have the users email as default.
Marwelln
The example I gave you was for a Iframe Canvas Application. Unfortunately, you cannot set the default email selection (between the user's email or facebook proxy email), this is controlled by facebook. I looked at my applications and they are all defaulted to the user's email address - so I am assuming this is some kind of protection from FB to make it harder for developers to collect email addresses until they have a "proven" application?
jusunlee