Facebook SDK 错误 Domain=com.facebook.sdk Code=5 在 ios 中获取好友列表时

Posted

技术标签:

【中文标题】Facebook SDK 错误 Domain=com.facebook.sdk Code=5 在 ios 中获取好友列表时【英文标题】:Facebook SDK Error Domain=com.facebook.sdk Code=5 when fetching friend list in ios 【发布时间】:2014-05-12 09:21:45 【问题描述】:

我是 ios 应用程序开发的新手。我想学习 ios 中的 facebook 集成和获取好友列表。

我遵循 git hub 代码https://github.com/facebook/facebook-ios-sdk/tree/master/samples/FriendPickerSample

并按照所有步骤进行操作。我的代码是:

- (IBAction)friendPicker:(id)sender 


    if (!FBSession.activeSession.isOpen)
    
        // permission : [NSArray arrayWithObjects:@"email", @"read_friendlists", @"user_photos", @"user_events", nil]
        [FBSession openActiveSessionWithReadPermissions:nil allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) 
            if(error)
            
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
                [alert show];

            
            else if (session.isOpen)
            
               // [FBSession setActiveSession:session];
                [self friendPicker:sender];
            
        ];
        return;
    

if(FBSession.activeSession.isOpen)
    
        FBRequest* friendsRequest = [FBRequest requestForMyFriends];
        [friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
                                                      NSDictionary* result,
                                                      NSError *error) 
            if(error)
            
                NSLog(@"error : %@",error);
            
            else
            

                NSArray* friends = [result objectForKey:@"data"];
                NSLog(@"total array : %@",result);
                NSLog(@"Found: %i friends", friends.count);
                for (NSDictionary<FBGraphUser>* friend in friends) 
                    NSLog(@"I have a friend named %@ with id %@", friend.name, friend.id);
                
            
        ];


    
  if(self.friendPickerController == nil)
    
        self.friendPickerController = [[FBFriendPickerViewController alloc] init];

        self.friendPickerController.title = @"Pick Friends";

        self.friendPickerController.delegate = self;
    

    [self.friendPickerController loadData];
    [self.friendPickerController clearSelection];

    [self presentViewController:self.friendPickerController animated:YES completion:nil];



-(void)fillTextAndDismiss:(NSString * )text

    [self.selectedFrdView setText:text];

    [self dismissViewControllerAnimated:YES completion:nil];


-(void)facebookViewControllerDoneWasPressed:(id)sender

    NSMutableString *text = [[NSMutableString alloc] init];

    for(id<FBGraphUser> user in self.friendPickerController.selection)
    
        if([text length])
        
            [text appendString:@", "];
        

        [text appendString:user.name];
    

    [self fillTextAndDismiss:text.length >0 ? text : @"<none>"];



-(void)facebookViewControllerCancelWasPressed:(id)sender

    [self fillTextAndDismiss:@"<cancelled>"];

我还创建了 facebookAppId 并且 plist 文件也由这个键更新。 然后我也收到此错误:

2014-05-12 14:21:27.043 fbFrndTest[2753:90b] error : Error Domain=com.facebook.sdk Code=5 "The operation couldn’t be completed. (com.facebook.sdk error 5.)" UserInfo=0xb166450 com.facebook.sdk:HTTPStatusCode=400, com.facebook.sdk:ParsedJSONResponseKey=
    body =     
        error =         
            code = 100;
            message = "(#100) Unknown fields: username.";
            type = OAuthException;
        ;
    ;
    code = 400;
, com.facebook.sdk:ErrorSessionKey=<FBSession: 0xb521fd0, state: FBSessionStateOpen, loginHandler: 0x0, appID: 1481684055397512, urlSchemeSuffix: , tokenCachingStrategy:<FBSessionTokenCachingStrategy: 0xb523240>, expirationDate: 2014-07-11 05:48:12 +0000, refreshDate: 2014-05-12 07:26:43 +0000, attemptedRefreshDate: 0001-12-30 00:00:00 +0000, permissions:(
)>

Github 代码运行成功,但是我的代码出错了。

【问题讨论】:

【参考方案1】:

我相信你应该取消注释

//[FBSession setActiveSession:session];

还要仔细检查您是否已在模拟器上的“设置”下登录 Facebook

【讨论】:

我已经检查过了,并且在没有评论该声明的情况下运行【参考方案2】:

使用FBRequest获取Facebook好友

Xcode 8.2.1 和目标

             FBRequest *friendRequest = [FBRequest requestForGraphpath:@"me/friends?fields=name,picture,birthday,email,location"];
             [friendRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error)
              
                  if(error == nil) 

                  NSArray *data = [result objectForKey:@"data"];
                  for (FBGraphObject<FBGraphUser> *friend in data)
                  
                     // [delegate.friendsListArray addObject:friend];
                      NSLog(@"%@:%@:%@:", [friend name],[friend birthday],[friend id]);
                      NSString *email=[friend objectForKey:@"email"];
                      NSLog(@"Email:%@",email);

                  
                      UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Facebook" message:@"Success in fetching Facebook Friends." delegate:self cancelButtonTitle:@"OK"otherButtonTitles:nil, nil];
                      [alert show];

                  
                  else
                  
                      UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Facebook" message:@"Problem in fetching Facebook Friends. Try Later!" delegate:self cancelButtonTitle:@"OK"otherButtonTitles:nil, nil];
                      [alert show];
                  


              ];

【讨论】:

以上是关于Facebook SDK 错误 Domain=com.facebook.sdk Code=5 在 ios 中获取好友列表时的主要内容,如果未能解决你的问题,请参考以下文章

Facebook SDK + Google SDK:被访问的框架将“document.domain”设置为“facebook.com”

iOS 6 facebook sdk 3.1.1 登录错误

Facebook iOS SDK 返回错误“com.facebook.sdk 错误 4”

Facebook Messenger sdk 贴纸

facebook sdk 仅公开分享

Facebook SDK:在IOS7上无法完成操作(com.facebook.sdk错误2)