簡體   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