[Facebook-iOS-SDK 4.0]如何从FBSDKProfile获取用户邮箱地址

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

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

没错。 FBSDKProfile 对象中不存在电子邮件地址。 您应该改用 FB 的 Graph API。 FB Developer 网站上有大量信息和代码示例 https://developers.facebook.com/docs/ios/graph

要回答您的问题,请尝试这样的操作。

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

更正的代码:

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

试试这个代码:

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

要获取电子邮件,您需要使用图表 API,特别是提供用您想要的字段填充的参数字段。查看 Facebook 的 Graph API 探索工具,它可以帮助找出查询。 https://developers.facebook.com/tools/explorer

我用来获取电子邮件的代码如下,假设您已经登录:

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

我想用我的发现来补充这一点。对于我的 FBSDKGraphRequest,我试图将我在登录时请求的权限作为字段传递。所以我将 fields 设置为:

"email,user_location"

但是,权限和字段不同。作为参考,这些是字段:https://developers.facebook.com/docs/graph-api/reference/v2.4/user

所以基于 link 你想通过:

"email,location"

希望对某人有所帮助!

来自 Facebook 开发者网站 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.

希望对大家有所帮助:)

这些参数给出了电子邮件

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

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