2013-01-07 40 views
2

讓我說清楚。我沒有使用Facebook SDK。我使用iOS SDK的Social.frameworkACAccountStore訪問Facebook帳戶,並使用它/他們發佈。iOS 6 SDK SLRequest返回'400'

我使用相同的代碼在Twitter上發佈。它工作100%。但由於某種原因,無論我爲Facebook整合做什麼,當我嘗試發佈信息時,都會收到「400」錯誤。

我的方法是:

ACAccountStore *account = [[ACAccountStore alloc] init]; 
ACAccountType *facebookAccountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; 

// Specify App ID and permissions 
NSDictionary *options = @{ ACFacebookAppIdKey: @"MY_APP_ID",ACFacebookPermissionsKey: @[@"publish_stream", @"publish_actions"],ACFacebookAudienceKey: ACFacebookAudienceFriends }; 
[account requestAccessToAccountsWithType:facebookAccountType options:options 
           completion:^(BOOL granted, NSError *error) 
{ 
    if (granted == YES) 
    { 
     NSDictionary *parameters = @{@"message": string999}; 
     NSURL *feedURL = [NSURL URLWithString:@"https://graph.facebook.com/me/feed"]; 

     SLRequest *feedRequest = [SLRequest 
            requestForServiceType:SLServiceTypeFacebook 
            requestMethod:SLRequestMethodPOST 
            URL:feedURL 
            parameters:parameters]; 

     acct.accountType = facebookAccountType; 

     // Post the request 
     [feedRequest setAccount:acct]; 

     // Block handler to manage the response 
     [feedRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) 
      { 
       if (granted && error == nil) { 
       } else { 
        NSLog(@"Facebook response, HTTP response: %i %@", [urlResponse statusCode], [error description]); 
        [self closeShareMenu]; 
       } 
      }]; 
    } 
} 

我不知道我要去哪裏錯了!這太討厭了!我已經在Facebook開發人員和所有人中正確設置了我的應用程序!請幫忙-_-'

+0

對不起,不知道格式太差了。 – topLayoutGuide

+0

responseData是什麼?如果你得到一個'400 - 錯誤的請求',也許你會從響應中得到一些指示。 – fguchelaar

+0

我不知道如何檢查responseData中的內容。 – topLayoutGuide

回答

1

繼昨天@fguchelaar與你之間舉行的聊天會之後,我能夠確定此問題的以下解決方案。

添加以下在iOS完成處理程序:

NSString *temp = [[NSString alloc] initWithData:data encoding: NSUTF8StringEncoding]; 
NSLog(temp); 
//'data' is your 'responseData' (or another object name) that you declare in your completion handler. 

這將讓你看到打印到調試控制檯問題的確切原因。現在根據提出的問題,您需要從iPhone SDK中調用此處理程序時生成的帳戶數組中獲取Facebook帳戶。由於Access Token可能會過期並給你這個'400'錯誤,所以不會在任何之前的階段。

對我而言;打印出來的錯誤是:error:{'400' A valid access token is required…這讓我非常惱火,因爲我之前訪問和檢查當前Twitter帳戶的方法是完美的。我的理論是,它應該對Facebook也同樣適用。如果我在一秒之前搶佔賬戶,爲什麼要立即撤銷訪問令牌?

我解決我的問題的方式(取決於您的錯誤原因,答案可能會有所不同)是使用for循環來檢查新創建的帳戶數組,唯一目的是找到相同的帳戶標識字符串作爲一個我保存到NSData/NSKeyedArchiver.

for(ACAccount *a in arrayOfAccounts) { 
    if([a.identifier isEqualToString:storedAccount.identifier]) { 
     //set the account to be used 
     accountToBeUsed = a; 

     //don't forget to break the For loop once you have your result. 
     break; 
    } else { 
     //This else{} block is not strictly necessary, but here you could set an account if no account was found with a matching identifier. 
    } 
} 

對於它的工作,建議在您的視圖控制器的.h文件中聲明一個ACAccount對象,添加@property@synthesize它,所以它可以在該分配for loop並在break;聲明後使用。

這有效地解決了我的整個問題'400'的錯誤。這是大約6小時我每天莫名其妙地討厭,所以我希望我的解釋可以幫助任何人誰發生過這個問題絆倒,和我的問題在這裏堆棧溢出:)

問候, cocotutch