简体   繁体   中英

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

I'm following the tutorials set out on the facebook SDK:

Login with facebook using the ios SDK

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. 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.

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:

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:

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.

- (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];
         }];

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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