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

ale*_*440 6 session facebook ios

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

}
Run Code Online (Sandbox Code Playgroud)

Soh*_*kar 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];
     }];
Run Code Online (Sandbox Code Playgroud)