简体   繁体   English

从iOS发布到Facebook时间轴可在首次尝试时使用HTTP 400

[英]Posting to Facebook timeline from iOS gives HTTP 400 on first try

I'm following the tutorials set out on the facebook SDK: 我正在按照facebook SDK上的教程进行操作:

Login with facebook using the ios SDK 使用ios SDK与Facebook登录

Publish to Feed 发布到提要

Everything seems to work okay, except on testing my app, I get HTTP error 400 (or error code 5) on my first try at attempting to posting to the facebook wall. 一切似乎都可以正常工作,除了在测试我的应用程序时,我在尝试发布到Facebook墙的第一次尝试时遇到HTTP错误400(或错误代码5)。 If I press my "Post" button again in the app, the second time, everything seems to work. 如果我再次在应用程序中按“发布”按钮,则第二次一切似乎正常。 On first try, the user is sent to the facebook app for authentication, and switches back to the app, then gives me the HTTP 400. On second try, there is no app switch, and the message is posted to the facebook wall as expected. 第一次尝试时,将用户发送到facebook应用程序进行身份验证,然后切换回该应用程序,然后为我提供HTTP400。第二次尝试时,没有应用程序切换,并且消息按预期发布到了Facebook墙上。

I'm trying to figure out why my app won't post to the wall/timeline on the first try. 我试图弄清楚为什么我的应用在第一次尝试时就不会发布到墙上/时间轴上。 My code is the same as that in the tutorial. 我的代码与教程中的代码相同。

Edit: 编辑:

Forgot to mention, I'm using one button to both authenticate and post to the wall - this is the code in the IBAction: 忘了提一下,我正在使用一个按钮同时进行身份验证和发布到墙上-这是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];
    }];

I solved this with mkral's helpful comments - code changed as follows: 我用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];
    }

So I first check if there's an active session - if there is I can just post to the wall/timeline, if not, I open a session. 因此,我首先检查是否有活动的会话-如果可以直接发布到墙上/时间线,如果没有,则打开一个会话。 I then registered for a notification (in the viewDidLoad) to let me know if there's a session change (in this case there would be if a session opened) and it would then post to the wall once authenticated. 然后,我注册了一个通知(在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];
         }];

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM