简体   繁体   中英

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

我正在将我的应用程序升级到Facebook-iOS-SDK-4.0 ,但似乎我无法从FBSDKProfile获取用户电子邮件,因为它只提供,用户名,用户ID等。

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

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

Code corrected:

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

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

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

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:)

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!

if ([FBSDKAccessToken currentAccessToken]) {
   [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"email"}]
    startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
      if (!error) {
                    }
  }];
}                    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM