4

I am getting access to user's facebook via:

[accStore requestAccessToAccountsWithType:fbAccountType
                                           options:options
                                        completion:^(BOOL granted, NSError *error) {
                                            if (granted) {
                                                // If access granted, then get the Facebook account info
                                                NSArray *accounts = [accStore
                                                                     accountsWithAccountType:fbAccountType];
                                                ACAccount *acc = [accounts lastObject];

                                                // Get the access token, could be used in other scenarios
                                                ACAccountCredential *fbCredential = [acc 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");
                                            }
                                        }];

This prints out:

 Facebook Access Token: (null)

I am granted access, but the token is null. What am I doing wrong?

Thanks

0xSina
  • 20,973
  • 34
  • 136
  • 253
  • i'm experiencing the same thing. did you ever find a solution? I couldn't reproduce myself but could with another teammate's phone. I found that if we went into Settings, disabled facebook access for our app and then immediately enabled it, we would then get a token. – djibouti33 Nov 13 '13 at 18:05
  • I am experiencing the same thing. But only on my iPhone. It works on other iPhone. – user4951 May 05 '14 at 11:13

2 Answers2

4

I've solved the problem by renewing the credentials. That is, after access to FB has been granted, but in the case where ACAccountCredential's oauthToken returns nil, we just call ACAccountStore's renewCredentialsForAccount. Credentials are renewed, and after that the token is valid!

1

Here's some info from Apple doc:

@property(nonatomic, retain) ACAccountCredential *credential Discussion This property is required and must be set before the account is saved. For privacy reasons, this property is inaccessible after the account is saved.

As in your code, [acc credential] will return null

For reference, http://developer.apple.com/library/ios/#documentation/Accounts/Reference/ACAccountClassRef/Reference/Reference.html#//apple_ref/doc/uid/TP40011019

Found a similar post here: Get Facebook access token from Social.framework(iOS6)

Hope this can help.

Community
  • 1
  • 1
zenglekidd
  • 21
  • 3
  • If you take look at the code in the link you provided, the code is the exact same. I am using the EXACT same code in the answer of the link. – 0xSina Jul 19 '13 at 04:34