Using facebook SDK on Android gives “User logged in as different Facebook user.” error

后端 未结 5 1029
Happy的楠姐
Happy的楠姐 2021-01-31 15:02

I\'m upgrading my use of the Facebook SDK to the latest version. The following code is prety much lifted line by line from Facebook\'s own examples, which can be found here: htt

相关标签:
5条回答
  • 2021-01-31 15:05

    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.

    0 讨论(0)
  • 2021-01-31 15:07
    if(LoginManager.getInstance()!=null){
            LoginManager.getInstance().logOut();
        }
    

    Write before "registerCallback();" method

    0 讨论(0)
  • 2021-01-31 15:20

    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.

    0 讨论(0)
  • 2021-01-31 15:30

    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:

    1. Check that you already have a valid access token (AccessToken.getCurrentAccessToken() != null), and don't request more permissions.
    2. 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.
    0 讨论(0)
  • 2021-01-31 15:31

    I also faced the same problem today. I got surpassed the problem by using below line of code when user clicks on FbLoginButton

    LoginManager.getInstance().logOut();

    0 讨论(0)
提交回复
热议问题