【问题标题】:Get Facebook access token from Social.framework(iOS6)从 Social.framework(iOS6) 获取 Facebook 访问令牌
【发布时间】:2012-12-05 12:08:20
【问题描述】:

我需要检索我在设置应用程序中设置的系统帐户的 Facebook 访问令牌。

我知道 Social.framework(iOS6) 知道我所有的 FB 帐户信息,并且我可以使用 SLRequest 类对 Graph API 执行 API 调用,就像在这篇文章中一样 http://blogs.captechconsulting.com/blog/eric-stroh/ios-6-tutorial-integrating-facebook-your-applications

但是,我的问题是 - 如何检索我的 Facebook 系统帐户的访问令牌?

我找到了 Twitter https://dev.twitter.com/docs/ios/using-reverse-auth 的解决方案,但你能帮我处理 Facebook

【问题讨论】:

    标签: ios facebook facebook-graph-api ios6


    【解决方案1】:

    如果您已经知道如何设置帐户存储,下面的代码显示了如何获取访问令牌:

    @property (strong, nonatomic) ACAccountStore *accountStore;
    @property (strong, nonatomic) ACAccount *fbAccount;
    
    ...
    
    @synthesize accountStore = _accountStore;
    @synthesize fbAccount = _fbAccount;
    
    ...
    
    // Initialize the account store
    self.accountStore = [[ACAccountStore alloc] init];
    
    // Get the Facebook account type for the access request
    ACAccountType *fbAccountType = [self.accountStore
        accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
    
    // Request access to the Facebook account with the access info
    [self.accountStore requestAccessToAccountsWithType:fbAccountType
                                               options:fbInfo
                                            completion:^(BOOL granted, NSError *error) {
        if (granted) {
            // If access granted, then get the Facebook account info
            NSArray *accounts = [self.accountStore
                                 accountsWithAccountType:fbAccountType];
            self.fbAccount = [accounts lastObject];
    
            // Get the access token, could be used in other scenarios
            ACAccountCredential *fbCredential = [self.fbAccount credential];            
            NSString *accessToken = [fbCredential oauthToken];
            NSLog(@"Facebook Access Token: %@", accessToken);
    
    
            // Add code here to make an API request using the SLRequest class
    
        } else {
            NSLog(@"Access not granted");
        }
    }];
    

    【讨论】:

    • 很遗憾我们不能使用 [self.fbAccount 凭证]; - 此方法返回 null - 只需查看文档 - developer.apple.com/library/ios/#documentation/Accounts/… - 出于隐私原因,保存帐户后无法访问此属性。
    • 有趣的是,我能够在模拟器和真实设备上打印出来,但我没有明确保存它,而且似乎它可能不会隐式发生。您是否保存它,如果保存,为什么需要保存?
    • 我想在 Facebook iOS SDK 中使用它。例如,如果我登录了系统,我不想再次使用 Facebook iOS SDK 登录,我只需要检索系统访问令牌并将其保存到 Facebook iOS SDK 中以供将来请求。
    • 如何保存到 Facebook iOS SDK?
    • twitter 访问令牌也一样
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 2012-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多