1

I am trying to build an iOS application with a custom token caching mechanism. I authenticate to FB on the device and I store the FB access token on my server against a session token on my server and then make FB API requests from the server.

The FB Login works fine. I am able to cache the token onto my server. But the real problem happens when I run the app the next time after I logged into FB.

This is the flow I follow to check if my app is logged into FB.

1) Hit a url on my server to get the FBAccessToken for a given session token for my application. This is an asynchronous request. 2) The asynchronous request callback is fired. If a token is present, cache it in my custom FBSessionTokenCachingStrategy class. 3) In the callback I initialize a FBSession object using:

FBSession *session = [[FBSession alloc] initWithAppID:nil permissions:@[@"basic_info"] urlSchemeSuffix:nil tokenCacheStrategy:self.tokenCache];

Then, I check the session.state. If I get a FBSessionCreatedTokenLoaded, then I decide to proceed with the openBehaviour method. However, I get a FBSessionStateCreated instead of FBSessionStateCreatedTokenLoaded even when a token has been cached on the server and the fetchFBAccessTokenData returns a FBAccessTokenData object.

Wonder why this happens?

Here is my implementation of the fetchFBAccessTokenData method of my custom FBSessionTokenCachingStrategy class:

-(FBAccessTokenData*) fetchFBAccessTokenData {

    NSLog(@"Fetching FB Access token");

    if (self.fbTokenData != nil) {
        NSLog(@"Found FB Token");

        FBAccessTokenData *fbTokenData = [FBAccessTokenData createTokenFromString:    [self.fbTokenData objectForKey:@"fb_token"] permissions:nil expirationDate:nil loginType:1 refreshDate:nil];

        NSLog(@"%@",fbTokenData.accessToken);

        return fbTokenData;
    }
    return nil;
}

It returns an FBAccessTokenData object but I still get an incorrect session state.

Does this happen because I am not returning the cached token correctly? I am implementing my code based on the standard example given for custom caching in the FB IOS SDK documentation.

Trupheenix
  • 15
  • 6

1 Answers1

1

It looks like your strategy is returning an FBAccessTokenData that has no permissions (which is not entirely unreasonable for your purposes unless you were to store permissions as well). When you init the session, however, specifying "basic_info" (while generally a good best practice, and required when asking for login that would prompt UI) will cause it to check that "basic_info" is included in the cached token. Since it is not, it will not load the cached token. For your purposes, you ought to be able to init the session with a nil permission array.

Chris Pan
  • 1,903
  • 14
  • 16
  • Adding permissions did the trick. It works now. Please update your documentation example with relevant information on how things work internally. Also update the document to include details on how logout is performed in case of a custom caching scheme. It's pretty straightforward to implement but would make life easy for some people. – Trupheenix Sep 06 '13 at 06:59
  • It's not clear how we can pass additional permissions. If I pass more than basic_info, the calls fail. What is happening here? – Trupheenix Oct 18 '13 at 11:56
  • Have a look at this link, you and @Trupheenix, will feed proud. [Stack Overflow](https://developers.facebook.com/docs/ios/troubleshooting) at the bottom of the page. – arunit21 Oct 06 '14 at 08:58