Varun Achar
Varun Achar

Reputation: 15109

Logged in user's email address

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?

Upvotes: 0

Views: 2981

Answers (2)

Nitzan Tomer
Nitzan Tomer

Reputation: 164297

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)

Upvotes: 2

Varun Achar
Varun Achar

Reputation: 15109

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"});

Upvotes: 0

Related Questions