2012-09-21 75 views
5

由于山狮现在已经officialy Facebook的整合,我想打一点点与Accounts.framework和OS X.在OS X山狮访问Facebook帐户

我建立了一个应用程序在Facebook开发应用程序和使用的Social.framework下面的代码请求访问登录的用户的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"); 
    } 
}]; 

现在我总是当我从Xcode中启动的应用程序出现错误Error Domain=XPCObjectsErrorDomain Code=2 "The operation couldn’t be completed. (XPCObjectsErrorDomain error 2.)"

第2-3次,OS X显示确认对话框“XYZ想要访问您的Facebook帐户”,我点击确定,我收到一个错误,说“应用程序尚未安装”。现在这个错误“消失了”,我得到了上面提到的错误。

我的问题是:我是否必须在我的Xcode Project(Info.plist)或Facebook Developer App中配置一些特殊的东西来使其与OS X一起使用?或者我在代码中丢失了什么?有关这方面的示例和教程似乎非常罕见: -/

我已经观看了有关Twitter/Facebook集成的WWDC 2012视频,但它几乎完全使用我使用的代码。

任何帮助表示赞赏:-)

干杯, 比约恩

更新
我解决了这个问题,我只是没有阅读文档不够仔细;-)我必须通过权限作为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:
我现在解决了我所有的问题,见我的回答如下。

回答

11

好吧,我终于解决了这个问题:-)

当你到一个用户的Facebook账号第一次请求访问,你可能只在Facebook的权限参考"User and Friend permissions"下所列的用户请求的权限。

一旦用户授予访问权限并且在Facebook上安装了该应用程序,则您可以请求您的应用程序所需的任何其他权限。重要的是你可以不是请求阅读一个接一个地写入权限!因此,在同一请求中请求权限read_streampublish_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"); 
    } 
}]; 

希望这会帮助别人:-)

+0

嗨,可能你可以回答我的问题吗? http://stackoverflow.com/questions/12644229/ios-6-facebook-posting-procedure-ends-up-with-permissions-alert-view – Stas

+0

虽然我尝试了你在这里发布的所有内容 - 我的意思是不同类型的权限..结果是一样的:( – Stas

+1

尼斯好好干得好!..它也帮了我很多 – Warewolf

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. 

未能这样做会导致相同的错误。