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

AndroidFacebookFacebook Graph-Api

Android Problem Overview


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: https://developers.facebook.com/docs/facebook-login/android/v2.3

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;

import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;

import java.util.Arrays;

public class TestFb extends FragmentActivity{

    CallbackManager callbackManager;
    FacebookController fbController;

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    fbController = FacebookController.getInstance(this);

    FacebookSdk.sdkInitialize(getApplicationContext());
    final LoginManager loginManager = LoginManager.getInstance();

    callbackManager = CallbackManager.Factory.create();
    loginManager.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                String userId = loginResult.getAccessToken().getUserId();
                String token = loginResult.getAccessToken().getToken();
                Log.d("fb", "ID: " + userId + " Token: "+ token);
            }

            @Override
            public void onCancel() {
                finish();
            }

            @Override
            public void onError(FacebookException e) {
                Log.d("fb", "Exception: " + e.getMessage());
            }
        });
        loginManager.logInWithReadPermissions(this, Arrays.asList(new String[]{"email", "user_likes"}));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }
}

The problem is that this does not work when the user has already logged in to the facebook app on his phone. In that case, this gives a "User logged in as different Facebook user" error.

I would think that given that there already is an active Facebook account on the device, a new one cannot be created. But if that was the case, should I get the user's ID and token from the existing account? I couldn't seem to be able to find any mention of this issue on Facebook's documentation pages.

Android Solutions


Solution 1 - Android

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 - Android

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 - Android

if(LoginManager.getInstance()!=null){
        LoginManager.getInstance().logOut();
    }

Write before "registerCallback();" method

Solution 4 - Android

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.

Solution 5 - Android

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();

Solution 6 - Android

Clear App cache and that should resolve the issue... I had the same error whenever i tried to login with the same account...but after clearing the App cache from settings, I was able to login.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionspacitronView Question on Stackoverflow
Solution 1 - AndroidsorianivView Answer on Stackoverflow
Solution 2 - AndroidlobzikView Answer on Stackoverflow
Solution 3 - AndroidParth DesaiView Answer on Stackoverflow
Solution 4 - AndroidMing LiView Answer on Stackoverflow
Solution 5 - AndroidRamesh 586View Answer on Stackoverflow
Solution 6 - AndroidArasan JView Answer on Stackoverflow