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

时间:2012-10-18 09:16:06

标签: ios facebook facebook-ios-sdk

我正在尝试创建一个Facebook Connect按钮,当我连接时,我需要为此获得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

我错过了什么吗?

1 个答案:

答案 0 :(得分:4)

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

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

把:

FBSession.activeSession = session;