由于Mountain Lion现已正式将Facebook整合,我想与OS X的Accounts.framework
和Social.framework
进行一些比较。
我在Facebook Developer App中设置了一个应用程序,并使用以下代码请求访问登录用户的Facebook帐户:
ACAccountStore *acStore = [[ACAccountStore alloc] init];
ACAccountType *accType = [acStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:FB_APP_ID, ACFacebookAppIdKey, @"read_stream, publish_stream", ACFacebookPermissionsKey, ACFacebookAudienceFriends, ACFacebookAudienceKey, nil];
[acStore requestAccessToAccountsWithType:accType options:options completion:^(BOOL granted, NSError *error) {
if (granted) {
NSLog(@"Accounts: %@", acStore.accounts);
NSLog(@"Access granted");
} else {
NSLog(@"Access denied");
}
}];
现在我总是收到错误Error Domain=XPCObjectsErrorDomain Code=2 "The operation couldn’t be completed. (XPCObjectsErrorDomain error 2.)"
当我从Xcode首次启动应用程序2-3次时,OS X显示确认对话框“XYZ想要访问您的Facebook帐户”,我点击确定,我收到一条错误,上面写着“应用程序还没有”安装”。此错误现在“消失”,我收到上述错误。
我的问题是:我是否必须在我的Xcode项目(Info.plist)或Facebook Developer App中配置一些特殊功能才能使其与OS X配合使用?或者我在代码中遗漏了什么?关于此的样本和教程似乎非常罕见: - /
我已经观看过关于Twitter / Facebook集成的WWDC 2012视频,但它几乎使用了我使用的代码。
感谢任何帮助: - )
干杯, 的Björn
更新
我解决了这个问题,我只是没有仔细阅读文档;-)我必须将权限作为NSArray而不是字符串传递。我已将选项字典修改为如下所示:
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
FB_APP_ID, ACFacebookAppIdKey,
[NSArray arrayWithObjects:@"read_stream", @"publish_stream", @"email", @"user_likes", nil], ACFacebookPermissionsKey,
ACFacebookAudienceFriends, ACFacebookAudienceKey, nil];
现在,OS X显示一条警告,询问我是否允许应用程序代表我发布到Facebook,我单击“确定”并出现新错误,导致无法访问Facebook帐户:
Error Domain=com.apple.accounts Code=7 "The Facebook server could not fulfill this access request: The app must ask for a basic read permission at install time."
我已将权限修改为仅email
或仅user_likes
,但这一切都导致了相同的错误。如果我仅请求访问read_stream
,则会授予访问权限,但会导致OAuth错误190并显示消息Error validating access token: User XXXXXX has not authorized application XXXXXX.
更新2:
我现在解决了所有问题,请参阅下面的答案。
答案 0 :(得分:11)
好吧,我终于解决了: - )
当您第一次请求访问用户的Facebook帐户时,您只能在Facebook的权限参考中请求"User and Friend permissions"下列出的用户的权限。
一旦用户授予访问权限并且在Facebook上也安装了该应用程序,您可以请求您的应用程序所需的任何其他权限。重要的是,您可以不同时要求读取和写入权限,一个接一个!因此,在同一请求中请求权限read_stream
和publish_stream
将无效。
这基本上就是我使用的代码:
self.store = [[ACAccountStore alloc] init];
ACAccountType *accType = [self.store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSMutableDictionary *options = [NSMutableDictionary dictionaryWithObjectsAndKeys:
FB_APP_ID, ACFacebookAppIdKey,
[NSArray arrayWithObjects:@"email", @"user_about_me", @"user_likes", nil], ACFacebookPermissionsKey, ACFacebookAudienceFriends, ACFacebookAudienceKey, nil];
[self.store requestAccessToAccountsWithType:accType options:options completion:^(BOOL granted, NSError *error) {
if (granted && error == nil) {
self.accounts = self.store.accounts;
[options setObject:[NSArray arrayWithObjects:@"read_stream, read_mailbox, read_requests", nil] forKey:ACFacebookPermissionsKey];
[self.store requestAccessToAccountsWithType:accType options:options completion:^(BOOL granted, NSError *error) {
if (granted && error == nil) {
completionHandler(granted, error);
} else {
NSLog(@"Access denied 2");
NSLog(@"%@", [error description]);
}
}];
} else {
NSLog(@"Error: %@", [error description]);
NSLog(@"Access denied");
}
}];
希望这会对某人有所帮助: - )
答案 1 :(得分:2)
我想补充一点,Facebook文档中还有一个警告:
// if a user has *never* logged into your app, you MUST include one of
// "email", "user_location", or "user_birthday". Other read
// permissions can also be included here.
如果不这样做会导致同样的错误。