Iphone 从iOS发布到Facebook timeline,第一次使用HTTP 400

Iphone 从iOS发布到Facebook timeline,第一次使用HTTP 400,iphone,ios,facebook,Iphone,Ios,Facebook,以下是facebook SDK上的教程: 除了测试我的应用程序外,一切似乎都正常工作。我第一次尝试在facebook墙上发帖时出现HTTP错误400(或错误代码5)。如果我在应用程序中再次按下“Post”按钮,第二次,一切似乎都正常。第一次尝试时,用户被发送到facebook应用程序进行身份验证,然后切换回该应用程序,然后给我HTTP 400。在第二次尝试时,没有应用程序切换,消息会按预期发布到facebook的墙上 我正试图弄明白为什么我的应用程序在第一次尝试时不会发布到墙上/时间线上。我

以下是facebook SDK上的教程:

除了测试我的应用程序外,一切似乎都正常工作。我第一次尝试在facebook墙上发帖时出现HTTP错误400(或错误代码5)。如果我在应用程序中再次按下“Post”按钮,第二次,一切似乎都正常。第一次尝试时,用户被发送到facebook应用程序进行身份验证,然后切换回该应用程序,然后给我HTTP 400。在第二次尝试时,没有应用程序切换,消息会按预期发布到facebook的墙上

我正试图弄明白为什么我的应用程序在第一次尝试时不会发布到墙上/时间线上。我的代码与教程中的代码相同

编辑:

忘了提及,我正在使用一个按钮进行身份验证并发布到墙上-这是IBAction中的代码:

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    // The user has initiated a login, so call the openSession method
    // and show the login UX if necessary.
    [appDelegate openSessionWithAllowLoginUI:YES];


    [FBRequestConnection startWithGraphPath:@"me/feed" parameters:postParams HTTPMethod:@"POST" completionHandler:^(FBRequestConnection *connection,id result,NSError *error)
    {

        NSString *alertText;

        if (error) {
             alertText = [NSString stringWithFormat:
                          @"error: domain = %@, code = %d", error.domain, error.code];
         } else {
             /*alertText = [NSString stringWithFormat:
                          @"Posted action, id: %@", [result objectForKey:@"id"]];*/
             alertText = @"Posted!";
         }
         // Show the result in an alert
        [[[UIAlertView alloc] initWithTitle:@"Facebook" message:alertText delegate:self cancelButtonTitle:@"OK!" otherButtonTitles:nil] show];
    }];

我用mkral的有用评论解决了这个问题-代码更改如下:

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

    if (FBSession.activeSession.isOpen) { //session is open so can post right away

        [FBRequestConnection startWithGraphPath:@"me/feed" parameters:postParams HTTPMethod:@"POST" completionHandler:^(FBRequestConnection *connection,id result,NSError *error)
         {

             NSString *alertText;

             if (error) {
                 alertText = [NSString stringWithFormat:
                              @"error: domain = %@, code = %d", error.domain, error.code];
             } else {
                 alertText = @"Posted!";
             }
             // Show the result in an alert
             [[[UIAlertView alloc] initWithTitle:@"Facebook" message:alertText delegate:self cancelButtonTitle:@"OK!" otherButtonTitles:nil] show];
         }];

    }
    else //session isn't open so authenticate first, then can post when back to app through notification
    {
        NSLog(@"Facebook Active Session not open");
        // The user has initiated a login, so call the openSession method
        // and show the login UX if necessary.
        [appDelegate openSessionWithAllowLoginUI:YES];
    }
因此,我首先检查是否有活动会话-如果有,我可以发布到墙/时间线,如果没有,我打开一个会话。然后,我注册了一个通知(在viewDidLoad中),让我知道是否有会话更改(在本例中,如果打开了会话,则会有更改),并在验证后将其发布到墙上

- (void)viewDidLoad
{
    [super viewDidLoad];        

//register for the session change notification you defined in the app delegate (for example when session changes to logged in)
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(sessionStateChanged:)
     name:FBSessionStateChangedNotification
     object:nil];

}

- (void)sessionStateChanged:(NSNotification*)notification {
    if (FBSession.activeSession.isOpen) {

        [FBRequestConnection startWithGraphPath:@"me/feed" parameters:postParams HTTPMethod:@"POST" completionHandler:^(FBRequestConnection *connection,id result,NSError *error)
         {

             NSString *alertText;

             if (error) {
                 alertText = [NSString stringWithFormat:
                              @"error: domain = %@, code = %d", error.domain, error.code];
             } else {
                 alertText = @"Posted!";
             }
             // Show the result in an alert
             [[[UIAlertView alloc] initWithTitle:@"Facebook" message:alertText delegate:self cancelButtonTitle:@"OK!" otherButtonTitles:nil] show];
         }];

    }
}

在您的第一次尝试中,您是否试图在用户通过身份验证之前发布到墙上?我认为这可能是问题所在-我使用一个按钮来进行身份验证和发布-忘记提及这一点。(更新了上面的帖子)你还没有按照教程开始,他们提到有一个登录/注销按钮和一个发布/共享按钮。。。一旦应用程序切换回来,你需要更新登录/注销按钮…嗯,我已经根据我的需要调整了教程-我使用一个按钮登录,然后发布。或者我需要单独登录,并单独发布吗?我会在验证后检查是否有委托方法。这样你就只有一个按钮了。单击时,检查它是否经过身份验证,如果是,则将数据发布到FB,如果未经过身份验证,则进行身份验证,并在委托方法中在身份验证后发布项目。