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.