从Social.framework(iOS6)获取Facebook访问令牌 [英] Get Facebook access token from Social.framework(iOS6)

查看:173
本文介绍了从Social.framework(iOS6)获取Facebook访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我知道Social.framework(iOS6)知道我的所有FB帐户信息&我可以使用SLRequest类来执行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

解决方案

如果您已经知道如何设置帐户商店,这里是代码,显示如何获取访问令牌:

  @property(strong,nonatomic)ACAccountStore * accountStore; 
@property(strong,nonatomic)ACAccount * fbAccount;

...

@synthesize accountStore = _accountStore;
@synthesize fbAccount = _fbAccount;

...

//初始化帐户存储
self.accountStore = [[ACAccountStore alloc] init];

//获取访问请求的Facebook帐户类型
ACAccountType * fbAccountType = [self.accountStore
accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

//请求访问具有访问信息的Facebook帐户
[self.accountStore requestAccessToAccountsWithType:fbAccountType
options:fbInfo
completion:^(BOOL approved,NSError *错误){
如果(被授予){
//如果访问权限,则获取Facebook帐户信息
NSArray * accounts = [self.accountStore
accountsWithAccountType:fbAccountType];
self.fbAccount = [accounts lastObject];

//获取访问令牌,可以在其他方案中使用
ACAccountCredential * fbCredential = [self.fbAccount凭据];
NSString * accessToken = [fbCredential oauthToken];
NSLog(@Facebook访问令牌:%@,accessToken);


//在此处添加代码,以使用SLRequest类生成API请求

} else {
NSLog(@访问未授予) ;
}
}];


I need to retrieve Facebook access token of my system account, which I set in Settings application.

I know that Social.framework(iOS6) knows all my FB account information & I can perform API calls to Graph API using SLRequest class, like in this post http://blogs.captechconsulting.com/blog/eric-stroh/ios-6-tutorial-integrating-facebook-your-applications

But, my question is - how can I retrieve my Facebook system account's access token?

I found solution for Twitter https://dev.twitter.com/docs/ios/using-reverse-auth , but could you help me with Facebook

解决方案

If you already know how to get the account store set up, here's code around it showing how to get the access token:

@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");
    }
}];

这篇关于从Social.framework(iOS6)获取Facebook访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆