6

我想从缓存的 tokenData 打开一个到 facebook 的会话

但我遇到了这个错误:原因:'FBSession:无法从当前状态的令牌数据打开会话'

我的代码:

 FBAccessTokenData *savedAccessTokenData =[TokenCacheStrategy getSavedToken];

if(savedAccessTokenData!nil){

 [appDelegate.session openFromAccessTokenData:savedAccessTokenData completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
                if(appDelegate.session.isOpen){
                    NSLog(@"session opened from saved access token");
                    NSLog(@"accesstoken: %@",[appDelegate.session.accessTokenData accessToken]);
                    if(completionBlock!=NULL){
                        completionBlock();
                    }

                }//session is open from token data

}
4

1 回答 1

1

访问令牌通常在规定的时间间隔后过期,例如。1小时 。如果有任何缓存的令牌信息,那么您可以尝试以自己的方式打开它,如果 if 块失败,那么在 else 块中,您可以尝试清除它并使用以下命令打开一个新会话:

// If the session state is any of the two "open" states when the button is clicked
if (FBSession.activeSession.state == FBSessionStateOpen
    || FBSession.activeSession.state == FBSessionStateOpenTokenExtended) {

    // Close the session and remove the access token from the cache
    // The session state handler (in the app delegate) will be called automatically
    [FBSession.activeSession closeAndClearTokenInformation];

    // If the session state is not any of the two "open" states when the button is clicked
} else {
    // Open a session showing the user the login UI
    // You must ALWAYS ask for basic_info permissions when opening a session
    [FBSession openActiveSessionWithReadPermissions:@[@"basic_info"]
                                       allowLoginUI:YES
                                  completionHandler:
     ^(FBSession *session, FBSessionState state, NSError *error) {
         [self sessionStateChanged:session state:state error:error];
     }];
于 2014-04-14T11:47:15.083 回答