Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reason: 'FBSession: cannot open a session from token data from its current state'

I want to open a session to facebook from a cached tokenData

but i fall on this error: reason: 'FBSession: cannot open a session from token data from its current state'

my code :

 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

}
like image 952
alex440 Avatar asked Oct 23 '13 23:10

alex440


1 Answers

Access tokens generally expire after a stipulated time interval eg. 1 hour . If theres any cached token info then you can try to open it your way and if the if block fails then in the else block you can try to clear it and open an new session using :

// 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];
     }];
like image 183
sshirgao Avatar answered Nov 03 '22 05:11

sshirgao