22

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

Rubycon
  • 18,156
  • 10
  • 49
  • 70

1 Answers1

24

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");
    }
}];
C Abernathy
  • 5,533
  • 1
  • 22
  • 27
  • 3
    Unfortunately we can't use [self.fbAccount credential]; - this method return null - just look at docs - http://developer.apple.com/library/ios/#documentation/Accounts/Reference/ACAccountClassRef/Reference/Reference.html#//apple_ref/doc/uid/TP40011019 - For privacy reasons, this property is inaccessible after the account is saved. – Rubycon Dec 27 '12 at 08:57
  • Interesting, I was able to print it out both on simulator and real device but I'm not saving it explicitly and it seems it may not be happening implicitly. Are you saving it and if so, why do you need to save? – C Abernathy Dec 27 '12 at 18:46
  • I would like to use it in the Facebook iOS SDK. For example, if I logged in in system, I don't want to login again using Facebook iOS SDK, I just need to retrieve system access token and save it into Facebook iOS SDK to future requests. – Rubycon Dec 04 '13 at 20:48
  • How to save it to Facebook iOS SDK? – user4951 Jan 10 '14 at 04:24
  • 3
    is this same for twitter acess token – Jitendra Apr 10 '14 at 05:02