Using facebook SDK on Android gives "User logged in as different Facebook user." error
Solution 1:
If you want to log in with the user that is now logged in on the Facebook app and you still have a token valid for a previous user, you can detect the error and log out in your FacebookCallback like this:
@Override
public void onError(FacebookException e) {
if (e instanceof FacebookAuthorizationException) {
if (AccessToken.getCurrentAccessToken() != null) {
LoginManager.getInstance().logOut();
}
}
}
Then you can log in again using the LoginManager.
Solution 2:
A lot of thanks to @sorianiv and @ming-li for their answers! I want to describe my case with more details because it can be helpful for somebody.
The Facebook SDK creates a shared preferences file called "com.facebook.AccessTokenManager.SharedPreferences". And when user signs in with facebook SDK stores access token, user name and other token info there. My issue was that I didn't call
LoginManager.getInstance().logOut()
when user signes out in my app. And the cache in shared preferences was not cleared. So when user than tries to sign in with different Facebook user Facebook SDK got the user data from the cache and returned error:
"User logged in as different Facebook user.”
In my case the solution was just to call logOut() when user signs out from my app.
Solution 3:
if(LoginManager.getInstance()!=null){
LoginManager.getInstance().logOut();
}
Write before "registerCallback();" method
Solution 4:
You would get this error if you already have a valid access token, but are requesting additional permissions (via the LoginManager.logInWithReadPermissions call), and the user logged into the FB app is different from the one you already have an access token for. You can do a couple of things:
- Check that you already have a valid access token (AccessToken.getCurrentAccessToken() != null), and don't request more permissions.
- If you do need to request more permissions, and you get this error, ask the user to log out of the FB app, and log in as themselves.