原因:'FBSession:无法从当前状态的令牌数据打开会话'

时间:2013-10-23 23:54:04

标签: ios facebook session

我想从缓存的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

}

1 个答案:

答案 0 :(得分: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];
     }];