让我说清楚。我没有使用Facebook SDK
。我正在使用iOS SDK的Social.framework
和ACAccountStore
来访问Facebook帐户,并使用它/它们发布。
我使用相同的代码在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 :(得分: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帐户。不在任何先前阶段,因为访问令牌可能会过期并给你这个'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.
}
}
要使其正常工作,建议在View Controller的.h文件中声明ACAccount
对象,添加@property
和@synthesize
,以便在{ {1}}并在for loop
语句之后使用。
这有效地解决了我的'400'错误的整个问题。这一天大约六个小时令人费解的令人费解,所以我希望我的解释可以帮助碰巧碰到这个问题的任何人,以及我在Stack Overflow上的问题:)
此致 cocotutch