Logged in user's email address

Tag: facebook , email Author: shan_cai Date: 2012-03-06

I'm unable to fetch the user's email address even though I've added the email in the extended permissions for the app.

This is the preview box that I get when i'm editing the app's details

Preview Box

Yet i'm unable to get the email address of the logged in user.

This is the url i'm using for fetching the user info

https://graph.facebook.com/me?fields=first_name,last_name,email,gender&access_token=+accessToken

This is the data i'm getting from facebook

{
   "first_name": "Varun",
   "last_name": "Achar",
   "gender": "male",
   "id": "1XXXXXX66660851"
}

Any ideas?

Does this happen to just your user or are you aware of other users which (with your app) got the same results? Also, with the same user, do you get the mail address here? (use the Graph API Explorer and just add the email permission)
No.. don't get the email address in the API explorer either
In the API explorer the email permission is part of extended permissions, but in the app edit page, it's part of the normal permissions. When I add the email extended permission in api explorer, i'm able to fetch the email address
App edit page? What do you mean? How are you asking for the permissions? Are you referring to the Authenticated Referrals?
Yes. In authenticated referrals email is part of User & Friend Permissions list, but in the Graph API Explorer the email is part of extender permissions. I'm able to get the email address in the Graph API Explorer but not through the app even though I've requested permission for email in the Authenticated Referrals page.

Best Answer

I don't know why the Authenticated Referrals approach is not working for you, but here's how you can ask for the email address and get it in 2 other ways (you'll need to deactivate the authenticated referrals though):

(1) Serveri-Side Authentication - When facebook load your canvas page it's doing that by POSTing to your canvas url, providing the *signed_request* parameter. With that you can check if the user is authenticated or not (if he is, you get the access token and other things about the user such as his fb user id). If the user is not authenticated you send him to the following url:

https://www.facebook.com/dialog/oauth?client_id=YOUR_APP_ID&redirect_url=YOUR_REDIRECT_URI&scope=email,publish_stream,etc

When the user gets back you should have an authenticated signed request.

(2) Client-Side Authentication - You use the javascript sdk to authenticate the user in your canvas page.

Once you have an access token you can get the email address by issuing a request to:

https://graph.facebook.com/me?fields=email

Or from the client side you can use:

FB.api("me?fields=email", function(response) {
    console.log(response);
});

Edit

You should read the javascript sdk documentation? especially the FB.login part. It shows an example of how to use it and asking for more permissions (using the the scope parameter)

comments:

Neither of the methods mentioned in the client-Side authentication provides the email address. I had tried this earlier too as mentioned in the question.
Have you read the javascript sdk documentation? especially the FB.login part? It shows an example of how to use it and asking for more permissions (using the the scope parameter)
Can you please shift the above comment to the answer so that I can accept it
Done. Though I think that it's ok to accept an answer if the actual thing you need is in the comments.
I ask you to shift it to the answers section for the benefit of others. Not everybody reads the comments. They might be confused why an accepted answer didn't work for them. Might lead to a down vote later on.. :)

Other Answer1

This is how I finally got it working. Couldn't get it working through the Authentication Referrals so had to do this

FB.login(function(resp)
{
  // Handle the response.
}
, {scope: "email, user_relationships"});