FBSDK没有返回用户的电子邮件

时间:2016-11-14 20:29:51

标签: ios objective-c facebook-graph-api

我查看了之前的所有帖子,但无法找到解决方案。我正在连接,它将返回除电子邮件之外的所有内容。最近的版本是否有一些变化,现在允许我接收它?

以下是相关代码 -

-(void)checkFacebookFriends {

[FBSDKAccessToken refreshCurrentAccessToken:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {


    if ([[FBSDKAccessToken currentAccessToken] hasGranted:@"user_friends"]) {

        [self makeFacebookFriendsRequest];
        // TODO: publish content.
    } else {

        FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
        loginManager.loginBehavior = FBSDKLoginBehaviorWeb;
        [loginManager logInWithReadPermissions:@[@"email", @"user_friends", @"public_profile"]
                            fromViewController:self
                                       handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
                                           //TODO: process error or result.

                                           if (error == nil) {
                                               [self makeFacebookFriendsRequest];
                                           }
                                           else {
                                               NSLog(@"no");

                                           }

                                       }];
    }
}];

}

-(void)makeFacebookFriendsRequest {

FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
                              initWithGraphPath:[NSString stringWithFormat:@"%@/friends", [FBSDKAccessToken currentAccessToken].userID]
                              parameters:@{@"fields": @"email,name,picture"}
                              HTTPMethod:@"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
                                      id result,
                                      NSError *error) {


    NSMutableArray *temp = [NSMutableArray new];

    for (NSDictionary *user in [result objectForKey:@"data"]) {
        FacebookUser *fbUser = [[FacebookUser alloc] initWithDictionary:user error:nil];
        [temp addObject:fbUser];

    }
//        self.facebookUsers = temp;

//        [self.tableView reloadData];

    // Handle the result
}];
}

1 个答案:

答案 0 :(得分:0)

如果您使用以下代码行编写权限,您将获得电子邮件,姓名,ID,出生日期。

[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me"
                                                        parameters:@{@"fields": @"id, name, email, gender,picture,age_range,birthday"}]
                      startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError * error)
                      {
                          if (!error)
                          {
                              NSString *fbAccessToken = [FBSDKAccessToken currentAccessToken].tokenString;
                              NSLog(@"the result is%@",result);


                          NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
                          [prefs setObject:[result objectForKey:@"name"]   forKey:@"Facebook_NAME"];
                          [prefs setObject:[result objectForKey:@"email"]  forKey:@"Facebook_EMAIL"];
                          [prefs setObject:fbAccessToken forKey:@"Facebook_TOKEN"];
                          [prefs synchronize];

                      }
                      else
                      {
                          NSLog(@"%@", [error localizedDescription]);

                      }

                  }];