ios - 使用我自己的 UIButton 获取用户 Facebook 信息

标签 ios facebook facebook-ios-sdk

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

-(IBAction)fbButtonClickHandler:(id)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 时它失败:

    Error: HTTP status code: 400
    FBSDKLog: Response  :
    The operation couldn’t be completed. (com.facebook.sdk error 5.)
    (null)
    error: Error Domain=com.facebook.sdk Code=5 "The operation couldn’t be completed.                   
    (com.facebook.sdk error 5.)" UserInfo=0x925f760 {com.facebook.sdk:ParsedJSONResponseKey=
    {type = mutable dict, count = 2,
     entries =>
      1 : {contents = "code"} =  
      {value = +400, type = kCFNumberSInt32Type}
      2 : {contents = "body"} = {type = mutable dict, count = 1,
       entries =>
       11 : {contents = "error"} = {type = mutable dict, count = 3,
       entries =>
    2 : {contents = "type"} = {contents = "OAuthException"}
        3 : {contents = "message"} = {contents = "An active access token must be used to query information about         
       the current user."}
    6 : {contents = "code"} = 2500

我错过了什么吗?

最佳答案

显然,FBRequestConnection 在 FBSession 中使用一个名为事件 session 的变量来使其在之后立即工作

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

放置:

FBSession.activeSession = session;

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

相关文章:

iphone - 防止 ASIHTTPRequest 遵循 HTTP 303 参见其他

ios - 如何防止此背景动画来自 'jumping'

ios - 将应用程序自动上传到Apple App Store

iphone - Facebook:发送 iOS 访问 token 服务器端并使用 App Secret 签名

ios - FB iOS SDK 3.2 : Why do I need an access token for logging a conversion pixel?

ios - 在 UIView 上应用渐变不起作用

facebook-java-sdk 无法创建 "Ad",表示在不真正使用任何东西时使用 instagram a/c 作为放置位置

facebook - 从本地主机测试 Facebook 应用程序

iphone - 使用图形 API 从 Facebook 获取用户信息

ios - 使用 Facebook 登录解析 : Server refused renewal request with error code: 190