问题描述
我正在尝试使用自定义令牌缓存机制构建iOS应用程序。我对设备上的FB进行身份验证,然后将FB访问令牌与服务器上的会话令牌相对应地存储在服务器上,然后从服务器发出FB API请求。
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.
FB登录工作正常。我能够将令牌缓存到我的服务器上。但是真正的问题发生在我登录FB后下一次运行该应用程序时。
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.
这是我检查应用程序是否已登录FB时遵循的流程。
This is the flow I follow to check if my app is logged into FB.
1)在我的服务器上单击一个URL,以获取应用程序给定会话令牌的FBAccessToken。这是一个异步请求。
2)异步请求回调被触发。如果存在令牌,请将其缓存在我的自定义FBSessionTokenCachingStrategy类中。
3)在回调中,我使用以下方法初始化FBSession对象:
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];
然后,我检查session.state。如果得到FBSessionCreatedTokenLoaded,则决定继续使用openBehaviour方法。但是,即使令牌已缓存在服务器上并且fetchFBAccessTokenData返回FBAccessTokenData对象,我也得到FBSessionStateCreated而不是FBSessionStateCreatedTokenLoaded。
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?
这是我自定义FBSessionTokenCachingStrategy类的fetchFBAccessTokenData方法的实现:
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;
}
它返回一个FBAccessTokenData对象,但是我仍然得到错误的会话状态。
It returns an FBAccessTokenData object but I still get an incorrect session state.
这是因为我没有正确返回缓存的令牌吗?我正在根据FB IOS SDK文档中为自定义缓存提供的标准示例来实现我的代码。
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.
推荐答案
策略将返回没有权限的FBAccessTokenData(除非您也要存储权限,否则这对您而言并非完全不合理)。但是,当您初始化会话时,指定 basic_info(通常是一个很好的最佳实践,并且在要求登录的提示提示UI时要求提供)将导致它检查 basic_info是否包含在缓存的令牌中。由于不是,因此不会加载缓存的令牌。为了您的目的,您应该能够使用nil权限数组来初始化会话。
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.
这篇关于FB iOS SDK自定义令牌缓存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!