objective-c [Facebook-iOS-SDK 4.0]如何从FBSDKProfile获取用户邮箱地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29323244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
[Facebook-iOS-SDK 4.0]How to get user email address from FBSDKProfile
提问by imcc
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.
我正在将我的应用程序升级到Facebook-iOS-SDK-4.0,但似乎无法从 获取用户电子邮件FBSDKProfile,因为它只提供用户名、用户 ID 等。
回答by genericdan
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
要获取电子邮件,您需要利用图形 API,特别是提供填充了您想要的字段的参数字段。看看 Facebook 的 Graph API 探索工具,它可以帮助找出查询。 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);
}];
回答by franciscodlp
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
没错。电子邮件地址不存在于 FBSDKProfile 对象中。您应该改用 FB 的 Graph API。FB 开发人员网站上有大量信息和代码示例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);
}
}];
}
回答by Aqib Mumtaz
Code corrected:
代码更正:
if ([FBSDKAccessToken currentAccessToken]) {
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result, NSError *error) {
if (!error) {
NSLog(@"fetched user:%@", result);
}
}];
}
回答by Dhaval H. Nena
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);
}
}];
}
}
}
}];
回答by Ted
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
从 Graph API v2.4 开始,/me 的 GET 请求应该包含一个明确的“字段”参数
回答by DaNLtR
From facebook developer site https://developers.facebook.com/docs/facebook-login/permissions/v2.2
来自 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.
请注意,即使您请求电子邮件权限,也不能保证您会获得电子邮件地址。例如,如果有人使用电话号码而不是电子邮件地址注册 Facebook,则电子邮件字段可能为空。
Hope it will help someone:)
希望它会帮助某人:)
回答by tvalent2
I wanted to add on to this with what I found. For my FBSDKGraphRequestI was trying to pass in the permissions I was requesting at login as the fields. So I was setting fieldsas something like:
我想用我发现的东西来补充这个。对于我,FBSDKGraphRequest我试图将我在登录时请求的权限作为字段传递。所以我设置fields为:
"email,user_location"
"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
但是,权限和字段是不同的。作为参考,这些是字段:https: //developers.facebook.com/docs/graph-api/reference/v2.4/user
So based on that link you want to pass:
因此,基于您要传递的链接:
"email,location"
"email,location"
Hope that helps someone!
希望对某人有所帮助!
回答by harish babu
if ([FBSDKAccessToken currentAccessToken]) {
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"email"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
}
}];
}

