查看和使用 Facebook 登录用户数据
Posted
技术标签:
【中文标题】查看和使用 Facebook 登录用户数据【英文标题】:Viewing and using Facebook login user data 【发布时间】:2016-05-21 23:10:36 【问题描述】:我按照 Facebook 提供的instructions 在我的应用程序中添加了“使用 Facebook 登录”按钮。为了填充其他视图控制器,我需要从登录用户那里提取数据,例如姓名和电子邮件。但是,似乎没有办法访问这些数据。
作为参考,以下代码直接从 Facebook 说明中粘贴。
FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];
loginButton.center = self.view.center;
loginButton.readPermissions = @[@"public_profile", @"email", @"user_friends"];
[self.view addSubview:loginButton];
非常感谢任何帮助。谢谢!
【问题讨论】:
您访问数据的代码在哪里?你检查过文档和 api 参考吗?有这方面的示例代码。另外,请阅读更改日志中的“声明性字段”。 是的——访问数据的代码正是我要问的。我在这里找到了“声明性字段”,但似乎并不完全相关:developers.facebook.com/docs/apps/changelog 官方文档中每个 api 端点都有示例代码,请查看 api 参考。和声明性字段肯定是相关的,没有声明性字段你只会得到 id 和 name。 【参考方案1】:如果您有自定义 Facebook 按钮,则只需在按钮操作上调用以下代码:
我已经检查过了,它对我来说很好用。 请参阅参考资料:
-(void)FBLogin
NSMutableDictionary *fbResultData;
fbResultData =[[NSMutableDictionary alloc]init];
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions: @[@"public_profile", @"email", @"user_friends"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
if (error)
NSLog(@"Process error");
else if (result.isCancelled)
NSLog(@"Cancelled");
else
NSLog(@"Logged in");
if ([FBSDKAccessToken currentAccessToken])
[[[FBSDKGraphRequest alloc] initWithGraphpath:@"me?fields=id,name,age_range,birthday,devices,email,gender,last_name,family,friends,location,picture" parameters:nil]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
if (!error)
NSString * accessToken = [[FBSDKAccessToken currentAccessToken] tokenString];
NSLog(@"fetched user:%@ ,%@", result,accessToken);
if ([result objectForKey:@"email"])
[fbResultData setObject:[result objectForKey:@"email"] forKey:@"email"];
if ([result objectForKey:@"gender"])
[fbResultData setObject:[result objectForKey:@"gender"] forKey:@"gender"];
if ([result objectForKey:@"name"])
NSArray *arrName;
arrName=[[result objectForKey:@"name"] componentsSeparatedByString:@" "];
[fbResultData setObject:[arrName objectAtIndex:0] forKey:@"name"];
if ([result objectForKey:@"last_name"])
[fbResultData setObject:[result objectForKey:@"last_name"] forKey:@"last_name"];
if ([result objectForKey:@"id"])
[fbResultData setObject:[result objectForKey:@"id"] forKey:@"id"];
[fbResultData setObject:accessToken forKey:@"Access Token"]; //m
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:[NSString stringWithFormat:@"me/picture?type=large&redirect=false"]
parameters:nil
HTTPMethod:@"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error)
if (!error)
if ([[result objectForKey:@"data"] objectForKey:@"url"])
[fbResultData setObject:[[result objectForKey:@"data"] objectForKey:@"url"] forKey:@"picture"];
NSLog(@"Final data of FB login********%@",fbResultData);
];
else
NSLog(@"result: %@",[error description]);
UIAlertController *alert=[UIAlertController alertControllerWithTitle:NSLocalizedString(@"Error", nil) message:[error description] preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *alertBtnOk=[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
[alert dismissViewControllerAnimated:YES completion:nil];
];
[alert addAction:alertBtnOk];
[self presentViewController:alert animated:YES completion:nil];
];
else
[[FBSDKLoginManager new] logOut];
];
在 fbResultData 中你会得到所有的用户数据,但是不要忘记导入这些文件:
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>
【讨论】:
真棒 - 所以自定义按钮是要走的路。很快就会报告结果。 好的,等待您的答复。 一个快速跟进问题 - 当我检查“朋友”字段时,data
键返回空白。这是我看到的:friends = data = ( ); summary = "total_count" = 640; ; ;
我想知道“数据”是否需要额外的权限
通过 user_friends 权限,您可以访问也使用您的应用的朋友列表,因此如果您的任何朋友都没有使用您的应用,则该列表将为空。见 user_friends 权限参考:developers.facebook.com/docs/facebook-login/permissions/…
代码有细微变化,将FBSDKGraphRequest中的“friends”替换为“invitable_friends”。【参考方案2】:
此代码对于获取未使用您的应用的 Facebook 登录用户数据和他/她的朋友列表很有用:
-(void)FBLogin
NSMutableDictionary *fbResultData;
fbResultData =[[NSMutableDictionary alloc]init];
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions: @[@"public_profile", @"email", @"user_friends"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
if (error)
NSLog(@"Process error");
else if (result.isCancelled)
NSLog(@"Cancelled");
else
NSLog(@"Logged in");
if ([FBSDKAccessToken currentAccessToken])
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me?fields=id,name,age_range,birthday,devices,email,gender,last_name,family,invitable_friends,location,picture" parameters:nil]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
if (!error)
NSString * accessToken = [[FBSDKAccessToken currentAccessToken] tokenString];
NSLog(@"fetched user:%@ ,%@", result,accessToken);
if ([result objectForKey:@"email"])
[fbResultData setObject:[result objectForKey:@"email"] forKey:@"email"];
if ([result objectForKey:@"gender"])
[fbResultData setObject:[result objectForKey:@"gender"] forKey:@"gender"];
if ([result objectForKey:@"name"])
NSArray *arrName;
arrName=[[result objectForKey:@"name"] componentsSeparatedByString:@" "];
[fbResultData setObject:[arrName objectAtIndex:0] forKey:@"name"];
if ([result objectForKey:@"last_name"])
[fbResultData setObject:[result objectForKey:@"last_name"] forKey:@"last_name"];
if ([result objectForKey:@"id"])
[fbResultData setObject:[result objectForKey:@"id"] forKey:@"id"];
[fbResultData setObject:accessToken forKey:@"Access Token"]; //m
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:[NSString stringWithFormat:@"me/picture?type=large&redirect=false"]
parameters:nil
HTTPMethod:@"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error)
if (!error)
if ([[result objectForKey:@"data"] objectForKey:@"url"])
[fbResultData setObject:[[result objectForKey:@"data"] objectForKey:@"url"] forKey:@"picture"];
NSLog(@"Final data of FB login********%@",fbResultData);
];
else
NSLog(@"result: %@",[error description]);
UIAlertController *alert=[UIAlertController alertControllerWithTitle:NSLocalizedString(@"Error", nil) message:[error description] preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *alertBtnOk=[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
[alert dismissViewControllerAnimated:YES completion:nil];
];
[alert addAction:alertBtnOk];
[self presentViewController:alert animated:YES completion:nil];
];
else
[[FBSDKLoginManager new] logOut];
];
【讨论】:
以上是关于查看和使用 Facebook 登录用户数据的主要内容,如果未能解决你的问题,请参考以下文章
将 Facebook 登录和地理位置数据插入 MongoDB
如何在使用 redux 登录 facebook/expo 和 firebase 后显示用户数据
通过从我的数据库表中发送用户名和密码来登录 Facebook
用户登录后如何使用社交 API(如 facebook)将数据存储在数据库中