LuckyLuke
LuckyLuke

Reputation: 49047

User not logged in error in Facebook SDK iOS

How can I fix this error in my program? Using Facebook SDK.

Error Domain=facebookErrDomain Code=10000 "The operation couldn’t be completed. (facebookErrDomain error 10000.)" UserInfo=0x5da9b10 {error=<CFBasicHash 0x5db09a0 [0x1087400]>{type = mutable dict, count = 2,
entries =>
    2 : <CFString 0x5db7920 [0x1087400]>{contents = "type"} = <CFString 0x5d35420 [0x1087400]>{contents = "OAuthException"}
    3 : <CFString 0x5d34970 [0x1087400]>{contents = "message"} = <CFString 0x5da96b0 [0x1087400]>{contents = "Error validating access token: The session is invalid because the user logged out."}
}
}

Upvotes: 3

Views: 6098

Answers (2)

Lio
Lio

Reputation: 4282

I think the solution enbr posted is not complete. I will explain why:

Access token errors are returned when the Facebook session expires and you continue using an old access token. Logout is not the only scenario in which sessions expire (e.g. change login password in the web). Hence, it is not enough to simply clean the user defaults after logout.

From the client side, we don't know if the access token we have is actually valid, until a response comes with such information. Therefore, you need to detect access token errors and restore the Facebook instance to a working state by cleaning the old access token and expiration date. This way, the user will need to login again to obtain a new access token.

So, IMO, what you need to do is:

  1. Handle errors and detect when an access token error has occurred.

  2. When an access token error occurs, logout automatically and clean user defaults to remove old access token (step 3).

  3. As a result of logout, you need to clean user defaults for access token and expiration date (as enbr posted).

Here you have some code that can be used to detect access token errors:

-(BOOL)isAccessTokenError:(NSError *) error {
    if ([[error domain] isEqualToString:@"facebookErrDomain"] && [error code] == 10000 ) {
        NSDictionary *userInfo = [error userInfo];
        NSDictionary *errorAsDictionary = [userInfo objectForKey:@"error"];
        if ([[errorAsDictionary objectForKey:@"type"] isEqualToString:@"OAuthException"]) {
            //Invalid access token
            return YES;         
        }
    }
    if ([[error domain] isEqualToString:@"facebookErrDomain"] && ([error code] == 110 || [error code] == 190)) {
        //Error accessing access token
        return YES;         
    }
    return NO;
}

The places to handle such errors are:

- (void)request:(FBRequest*)request didFailWithError:(NSError*)error; 
- (void)dialog:(FBDialog*)dialog didFailWithError:(NSError *)error;

I hope this helps.

UPDATE: I forgot to mention. If you are using the SSO feature (most likely), I think it is a very good idea to clean the facebook cookies before login. Sometimes, after an invalid access token error, it seems login won't bring back the Facebook object to a working state (valid access token) unless a "clean login" is performed. Not always work though.

Also, If you are not using the SSO feature, this used to fix the ghost login popup that appeared to automatically disappear again.

This is how I clean the cookies:

NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    NSArray* facebookCookies = [cookies cookiesForURL:
                                [NSURL URLWithString:@"http://login.facebook.com"]];    
    for (NSHTTPCookie* cookie in facebookCookies) {
        [cookies deleteCookie:cookie];
    }
enter code here

Upvotes: 8

enbr
enbr

Reputation: 1203

I just recently came across this error also. Here is the solution. Place this code in your main FBSessionDelegate (probably your app delegate).

- (void)fbDidLogout {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults removeObjectForKey:@"FBAccessTokenKey"];
    [defaults removeObjectForKey:@"FBExpirationDateKey"];
    [defaults synchronize];
}

Then delete and reinstall the app!

Upvotes: 1

Related Questions