Facebook帐户访问权限,但OauthToken是空的

时间:2013-11-26 04:32:18

标签: objective-c facebook ios7 acaccount acaccountstore

我的应用仅限iOS7,为了避免头痛,我决定不使用Facebook SDK进行身份验证,而是依赖iOS的ACAccountStore。不幸的是,我仍然感到头痛。

我有一个名为FacebookService的类,其中一个方法名为login。我将在下面发布代码,但一般情况下,我会执行配置ACAccountStore实例的标准舞蹈,然后调用requestAccessToAccountsWithType。在完成块中,如果granted返回false,则向用户发送消息并发送短路。

如果granted为真,那么我的代码用于假设我拥有使用以下代码获取用户的oauthToken所需的一切:

ACAccount *fbAccount = [[accountStore accountsWithAccountType:fbAccountType] lastObject];
ACAccountCredential *fbCredential = [fbAccount credential];
NSString *accessToken = [fbCredential oauthToken];

我从oauthToken获得了ACAccount#credential,而第一件奇怪的事情是according to the documentation

  

出于隐私原因,此属性(保存帐户后无法访问。

我可以肯定地告诉你,大多数时候,这个属性是绝对可访问的,我可以使用它发送到我的服务器找到以前认证的用户。

然而,有几次,该值实际上将为空。并且在发生这种情况的每种情况下,如果用户转到设置>则可以解决该问题。 Facebook>关闭我们的应用>打开我们的应用程序。他们回到我们的应用程序,点击“使用Facebook登录”,一切正常。

所以这是我的问题:

  1. 有没有办法从FB ACAccount持续获取用户的oauthToken?正如您在我的代码中看到的那样,我现在首先检查该值是否为空,如果是,则向其发出警报。
  2. 用户可以通过进入“设置”,禁用然后启用我的应用来解决此问题?他们的帐户不同步还是我需要续订凭据?
  3. 如果我被告知已经授予了访问但是我没有收到oauthToken,那么有没有办法在my方法中恢复?
  4. 有没有人知道我可以设置系统的方法,以便我可以可靠地重现这个?
  5. 这是我的代码:

    - (void)login
    {
        ACAccountStore *accountStore = [[ACAccountStore alloc] init];
        ACAccountType *fbAccountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
        NSString *appId = [[Environment sharedInstance] fbAppId];
    
        NSDictionary *fbOptions = @{
            ACFacebookAppIdKey : appId,
            ACFacebookPermissionsKey : [@"email,user_birthday,user_hometown,user_location,user_photos" componentsSeparatedByString:@","],
            ACFacebookAudienceKey: ACFacebookAudienceEveryone
        };
    
        [accountStore requestAccessToAccountsWithType:fbAccountType options:fbOptions completion:^(BOOL granted, NSError *error) {
            NSString *message;
            NSString *title;
    
            if (!granted) {
                if (!error || error.code == ACErrorPermissionDenied) {
                    title = @"AFAR Needs Your Permission";
                    message = @"Your Facebook account is configured in Settings. Go to Settings and enable AFAR.";
                } else if (error.code == ACErrorAccountNotFound) {
                    title = @"No Facebook Account";
                    message = @"There are no Facebook accounts configured. You can add or create a Facebook account in Settings.";
                }
    
                if (message) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [[NSNotificationCenter defaultCenter] postNotificationName:AfarFacebookLoginDidNotSucceed object:nil];
                        [UIAlertView simpleAlertWithTitle:title message:message];
                    });
                }
    
                return;
            }
    
            ACAccount *fbAccount = [[accountStore accountsWithAccountType:fbAccountType] lastObject];
            ACAccountCredential *fbCredential = [fbAccount credential];
            NSString *accessToken = [fbCredential oauthToken];
    
            if ([accessToken isEmpty]) {
                message = @"You need to allow AFAR access to your Facebook account. Please visit Settings > Facebook and look for the AFAR entry. Turn the AFAR setting off, then back on.";
                title = @"Problem Connecting to Facebook";
    
                dispatch_async(dispatch_get_main_queue(), ^{
                    [[NSNotificationCenter defaultCenter] postNotificationName:AfarFacebookLoginDidNotSucceed object:nil];
                    [UIAlertView simpleAlertWithTitle:title message:message];
                });
    
                return;
            }
    
            [SessionAPIService signInUsingFacebookToken:accessToken withBlock:^(NSError *error) {
                if (!error) {
                    [[NSNotificationCenter defaultCenter] postNotificationName:AFARCurrentUserDidSignInNotificatioName object:nil];
                }
            }];
        }];
    }
    

0 个答案:

没有答案