OGeek|极客世界-中国程序员成长平台

标题: ios - 使用我自己的 UIButton 获取用户 Facebook 信息 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 23:12
标题: ios - 使用我自己的 UIButton 获取用户 Facebook 信息

我正在尝试创建一个 Facebook 连接按钮,我需要在连接时为此获取 FBGraphUser。我的 Button 上有以下代码:

-(IBAction)fbButtonClickHandlerid)sender {
    mainDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];

  if (mainDelegate.fbSession.isOpen) { 
    [mainDelegate.fbSession closeAndClearTokenInformation]; // FB delog
  }
  else {
    if (mainDelegate.fbSession.state != FBSessionStateCreated) {
        mainDelegate.fbSession = [[FBSession alloc] initWithPermissions:[NSArray arrayWithObjects"email", @"publish_actions", @"user_birthday",nil]];
    }
     // loggin on facebook
    [mainDelegate.fbSession openWithCompletionHandler:^(FBSession *session,
                                                     FBSessionState status,
                                                     NSError *error) {

        [fbButton setTitle:[self updateFbButtonLabel] forState:UIControlStateNormal];
        // reading the documentation i'm trying this to get the FBGraphUser
        if(session.isOpen) {
            [FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id<FBGraphUser> user, NSError *error) {

                  if (!error) {
                    NSString *userInfo = @"";

                    // Example: typed access (name)
                    // - no special permissions required
                    userInfo = [userInfo
                                stringByAppendingString:
                                [NSString stringWithFormat"Name: %@\n\n",
                                 user.name]];

                    // Example: typed access, (birthday)
                    // - requires user_birthday permission
                    userInfo = [userInfo
                                stringByAppendingString:
                                [NSString stringWithFormat"Birthday: %@\n\n",
                                 user.birthday]];
                    // Display the user info
                    NSLog(@"%@", userInfo);
                  }
                  NSLog(@" error: %@" , error);
                }];
        }
       }
    }];
}
}

问题是当我调用 startForMeWithCompletionHandler 时它失败了:

 错误:HTTP 状态码:400
    FBSDKLog:响应:
    操作无法完成。 (com.facebook.sdk 错误 5。)
    (无效的)
    错误:错误域=com.facebook.sdk 代码=5“操作无法完成。
    (com.facebook.sdk 错误 5。)“UserInfo=0x925f760 {com.facebook.sdkarsedJSONResponseKey=
    {type = 可变字典,count = 2,
     条目 =>
      1:{内容=“代码”}=
      {值 = +400,类型 = kCFNumberSInt32Type}
      2 : {contents = "body"} = {type = mutable dict, count = 1,
       条目 =>
       11 : {contents = "error"} = {type = mutable dict, count = 3,
       条目 =>
    2 : {contents = "type"} = {contents = "OAuthException"}
        3 : {contents = "message"} = {contents = "必须使用事件访问 token 来查询有关信息
       当前用户。"}
    6 : {内容 = "代码"} = 2500

我错过了什么吗?



Best Answer-推荐答案


显然,FBRequestConnection 在 FBSession 中使用了一个名为 active session 的变量来使其在之后工作

 [mainDelegate.fbSession openWithCompletionHandler:^(FBSession *session,
                                                     FBSessionState status,
                                                     NSError *error) {

放:

FBSession.activeSession = session;

关于ios - 使用我自己的 UIButton 获取用户 Facebook 信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12951135/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4