FB iOS SDK Custom Token Caching issue

懵懂的女人 提交于 2020-01-03 04:12:05

问题


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.


回答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.



来源:https://stackoverflow.com/questions/18642142/fb-ios-sdk-custom-token-caching-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!