繁体   English   中英

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

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

我正在按照facebook SDK上的教程进行操作:

使用ios SDK与Facebook登录

发布到提要

一切似乎都可以正常工作,除了在测试我的应用程序时,我在尝试发布到Facebook墙的第一次尝试时遇到HTTP错误400(或错误代码5)。 如果我再次在应用程序中按“发布”按钮,则第二次一切似乎正常。 第一次尝试时,将用户发送到facebook应用程序进行身份验证,然后切换回该应用程序,然后为我提供HTTP400。第二次尝试时,没有应用程序切换,并且消息按预期发布到了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];
         }];

    }
}

暂无
暂无

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

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