[Facebook-iOS-SDK 4.0]How to get user email address from FBSDKProfile
Asked Answered
T

8

45

I'm upgrading my app to Facebook-iOS-SDK-4.0, but seems like I can't get user email from FBSDKProfile, since it only provide, username, userid, etc.

Tramroad answered 28/3, 2015 at 22:14 Comment(3)
I have tried the same code as in your accepted answer however I am not getting email id though I have 'email' permission. I am getting only user id and username in result parameter.Ddt
Issue resolved in duplicate post: https://mcmap.net/q/358329/-ios-facebook-sdk-login-doesn-39-t-return-email-despite-permissions-grantedDendro
I have not yet tried it, but it seems legit. Thanks @DendroTramroad
G
129

To fetch email you need to utilize the graph API, specifically providing the parameters field populated with the fields you want. Take a look at Facebook's Graph API explore tool, which can help to figure out the queries. https://developers.facebook.com/tools/explorer

The code that worked for me to fetch email is the following, which assumes you are already logged in:

    NSMutableDictionary* parameters = [NSMutableDictionary dictionary];
    [parameters setValue:@"id,name,email" forKey:@"fields"];

    [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:parameters]
     startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
                                  id result, NSError *error) {
         aHandler(result, error);
     }];
Geraldgeralda answered 14/7, 2015 at 13:54 Comment(3)
Deserves more votes than accepted answer... answers the Email requirement.. +1!!!Trager
Is it mandatory to login with facebook if you just want to get the user email?Hyderabad
Please what are all the fields in the result ?Innocuous
M
28

That's correct. The email address is not present in the FBSDKProfile object. You should use FB's Graph API instead. There is plenty of info and code samples at the FB Developer site https://developers.facebook.com/docs/ios/graph

To answer your question, try something like this.

if ([FBSDKAccessToken currentAccessToken]) {
   [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"email"}]
    startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
      if (!error) {
         NSLog(@"user:%@", result);       
      }
  }];
}
Mull answered 29/3, 2015 at 1:45 Comment(14)
that's what I'm doing right now, but it is waste of resource.Tramroad
This request does not have email in the result.Lewert
I tried to login like this: fbLoginManager.logInWithReadPermissions(["email", "public_profile"] ... and the email is still not sent in the result object. Anyone knows how to do it?Benediction
@BencePattogato you can up vote this ticket for more people to see it.Tramroad
@BencePattogato We'd need further details to help you out. I just tried the code I posted using the permissions ["public_profile", "email", "user_friends"] and I'm receiving the user's email in the results. You may want to check FBSDKAccessToken properties (declinedPermissions, permissions) to make sure you actually have the required permission.Mull
@BencePattogato That's correct. You should declare "email" in requested permissions and later you'll get email within FBSDKGraphRequest resultDeflagrate
I believe fb should have given it in profile directlyDialogue
I have tried the same code however I am not getting email id though I have 'email' permission. I am getting only user id and username in result parameter.Ddt
with nil parameters you'll get only userId and name, for email-id send ["fields":"email"] in parametersSanderson
Issue resolved in duplicate post: https://mcmap.net/q/358329/-ios-facebook-sdk-login-doesn-39-t-return-email-despite-permissions-grantedDendro
Actually I have found the solution. For latest Graph api version we need to pass parameters in graphpath ie. @"me/emailId?location" explicitly. It was not mentioned in their documentation.Ddt
u saved my life @GauravRanaHandbarrow
@GauravRana I tried looking in Facebook doc ref for parameter names, unable to find any.... Do you have a link to a parameter list?Lubricator
Please note that in recent versions of Facebook SDK you require to specify parameters. Otherwise you will receive a warning like this: FBSDKLog: starting with Graph API v2.4, GET requests for /me should contain an explicit "fields" parameterEckardt
D
6

Code corrected:

  if ([FBSDKAccessToken currentAccessToken]) {
    [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil]
        startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
                                     id result, NSError *error) {
          if (!error) {
            NSLog(@"fetched user:%@", result);
          }
        }];
  }
Dialogue answered 27/4, 2015 at 13:13 Comment(3)
I have tried the same code however I am not getting email id though I have 'email' permission. I am getting only user id and username in result parameter.Ddt
sorry for late reply, make sure your email address is verified for facebook account, if not then you may need to resend varification email from your facebook account and click on email verification linkDialogue
Actually I have found the solution. For latest Graph api version we need to pass parameters in graphpath ie. @"me/emailId?location" explicitly. It was not mentioned in their documentation.Ddt
C
5

Try this code :

FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
    if (error) {
        // Process error
    } else if (result.isCancelled) {
        // Handle cancellations
    } else {
        // If you ask for multiple permissions at once, you
        // should check if specific permissions missing

        if ([result.grantedPermissions containsObject:@"email"]) {
            if ([FBSDKAccessToken currentAccessToken]) {
                [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil]
                 startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
                     if (!error) {
                         NSLog(@"fetched user:%@", result);
                     }
                 }];
            }
        }
    }
}];
Chrominance answered 30/5, 2015 at 11:4 Comment(2)
I have tried the same code however I am not getting email id though I have 'email' permission. I am getting only user id and username in result parameter.Ddt
Make sure your email has been verified by clicking on the link sent by facebook, this may also cause issue.Dialogue
C
3

These parameters gives the email

NSDictionary *parameters = @{@"fields":@"email"};

[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:parameters]
 startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) 
{
    NSLog(@"fetched user:%@", result);
}];

starting with Graph API v2.4, GET requests for /me should contain an explicit "fields" parameter

Countersubject answered 4/1, 2016 at 8:54 Comment(0)
L
2

From facebook developer site https://developers.facebook.com/docs/facebook-login/permissions/v2.2

Note, even if you request the email permission it is not guaranteed you will get an email address. For example, if someone signed up for Facebook with a phone number instead of an email address, the email field may be empty.

Hope it will help someone:)

Lanni answered 15/11, 2015 at 12:36 Comment(0)
T
1

I wanted to add on to this with what I found. For my FBSDKGraphRequest I was trying to pass in the permissions I was requesting at login as the fields. So I was setting fields as something like:

"email,user_location"

However, permissions and fields are different. For reference, these are fields: https://developers.facebook.com/docs/graph-api/reference/v2.4/user

So based on that link you want to pass:

"email,location"

Hope that helps someone!

Turkish answered 23/8, 2015 at 18:26 Comment(0)
T
0
if ([FBSDKAccessToken currentAccessToken]) {
   [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"email"}]
    startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
      if (!error) {
                    }
  }];
}                    
Tetrapod answered 17/4, 2019 at 6:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.