Facebook iOS SDK不返回用户电子邮件

时间:2012-04-25 14:13:43

标签: iphone ios facebook email sdk

我正在尝试访问某些用户的信息,包括电子邮件。为此,我使用Graph API来调用此方法:

[facebook requestWithGraphPath:@"me" andDelegate:self];

它返回用户信息,没有电子邮件信息(生日也缺失)。我设置了所有权限并使用多个帐户(其中包含电子邮件和生日作为公共信息)进行了测试,并接受了所有对话框的请求。

这是我的初学者:

if(!facebook) {
    facebook = [[Facebook alloc] initWithAppId:[self _APP_KEY] andDelegate:self];
    NSArray *array =  [NSArray arrayWithObjects: @"email", @"user_birthday", @"publish_stream", @"offline_access", nil];
    [self setPermissions:array];
}

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"] && [defaults objectForKey:@"FBExpirationDateKey"]) {
    facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
    facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}

if (![facebook isSessionValid]) {
    [facebook authorize:permissions];
} 
else {
    [self getUserInfo];
}

登录回调:

- (void)fbDidLogin {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
    [defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
    [defaults synchronize];

    [self getUserInfo];
}

getUserInfo方法:

- (void) getUserInfo {
    [facebook requestWithGraphPath:@"me" andDelegate:self];
}

请求回调:

 - (void)request:(FBRequest *)request didLoad:(id)result {

    if ([result isKindOfClass:[NSDictionary class]]) {
        NSDictionary* json = result;
        NSLog(@"-> %@", json);
    }
}

结果如下:

-> {
    "first_name" = EDITED (my name);
    gender = male;
    hometown =     {
        id = 111072692249998;
        name = "Porto Alegre";
    };
    id = 653099452;
    languages =     (
                {
            id = 104034306299811;
            name = "Brazilian Portuguese";
        },
                {
            id = 106059522759137;
            name = English;
        }
    );
    "last_name" = EDITED (my last name);
    link = EDITED (my profile);
    locale = "en_US";
    location =     {
        id = 112047398814697;
        name = "S\U00e3o Paulo, Brazil";
    };
    name = EDITED (my name);
    timezone = "-3";
    "updated_time" = "2012-04-25T13:36:51+0000";
    verified = 1;
}

如您所见,没有关于用户电子邮件的信息。我错过了一步吗?任何想法?

提前致谢

7 个答案:

答案 0 :(得分:7)

尝试使用它,它适用于我的情况

  

facebook = [[Facebook alloc] initWithAppId:[self _APP_KEY] andDelegate:self];       [facebook授权:[NSArray arrayWithObjects:@“email”,nil]];

答案 1 :(得分:3)

试试这个 permissions = [[NSArray alloc] initWithObjects:@“offline_access”,@“email”,nil];

答案 2 :(得分:1)

在fbDidlogin触发调用此方法之后,您可以获得此类用户的电子邮件

- (void)fetchUserDetails {
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   @"SELECT uid, name, pic, email FROM user WHERE uid=me()", @"query",nil];

    [fb requestWithMethodName:@"fql.query"
                                     andParams:params
                                 andHttpMethod:@"POST"
                                   andDelegate:self];
}

request:(FBRequest *)request didLoad你会做这样的事情:

- (void)request:(FBRequest *)request didLoad:(id)result 
{

  if([request.url rangeOfString:@"fql.query"].location !=NSNotFound)
    {
        if ([result isKindOfClass:[NSArray class]] && [result count]>0) {
            result = [result objectAtIndex:0];
        }

        if ([result objectForKey:@"email"]) {

            NSLog(@"%@",[result objectForKey:@"email"]);
           }
      }
}

享受:)

答案 3 :(得分:1)

1)获得获取电子邮件和信息等的许可......

// You must ALWAYS ask for public_profile permissions when opening a session
                [FBSession openActiveSessionWithReadPermissions:@[@"public_profile",@"user_videos",@"email"]
                                                   allowLoginUI:YES
                                              completionHandler:
                 ^(FBSession *session, FBSessionState state, NSError *error)
                 {
                     // Retrieve the app delegate
                     AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
                     // Call the app delegate's sessionStateChanged:state:error method to handle session state changes
                     [appDelegate sessionStateChanged:session state:state error:error];
                 }];

2)获取您想要的电子邮件地址

if (FBSession.activeSession.isOpen)
    {
        [[FBRequest requestForMe] startWithCompletionHandler:
         ^(FBRequestConnection *connection,
           NSDictionary<FBGraphUser> *user,
           NSError *error)
        {
             if (!error)
             {
                 NSString *userEmail = [user objectForKey:@"email"];
             }
            [tblSettingVw reloadData];
         }];
    }

注意:用户词典将在

之后返回
{
    "first_name" =;
    gender =;
    id =;
    "last_name" =;
    link = "";
    locale = "";
    name = "";
    timezone = "";
    "updated_time" = "";
    verified = ;
}

答案 4 :(得分:1)

通过 电话号码 而不是电子邮件验证的所有Facebook帐户将获得NULL的{​​{1}}

答案 5 :(得分:0)

如果您获得了用户的授权,则只能访问用户的电子邮件。

公共图表不提供此信息。

您应该阅读:https://developers.facebook.com/docs/guides/web/#login

答案 6 :(得分:0)

您需要通过以下方式向用户请求获取此信息:

- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
NSArray *permissions = [[NSArray alloc] initWithObjects:
                        @"user_birthday",
                        nil];
return [FBSession openActiveSessionWithPermissions:permissions
                                      allowLoginUI:allowLoginUI
                                 completionHandler:^(FBSession *session,
                                                     FBSessionState state,
                                                     NSError *error) {
                                     [self sessionStateChanged:session
                                                         state:state
                                                         error:error];
                                 }];

}

以下是您需要的所有信息,请按照本教程

进行操作

首先,这一个:Facebook Login

然后,这一个:Fetch user data