Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 973
spacitron Avatar asked Apr 22 '15 08:04

spacitron


People also ask

How did you integrate the Facebook SDK in Android app?

To use the Facebook SDK in an Android Studio project, add the SDK as a build dependency and import the SDK. Go to Android Studio | New Project | Minimum SDK. Select API 15: Android 4.0. 3 (IceCreamSandwich) or higher and create your new project.

What is Facebook SDK used for?

The Facebook SDK is a set of software components that developers can include in their mobile app to understand how people use the app, run optimized marketing campaigns and enable Facebook login and social sharing. This course helps you understand the purpose of the Facebook SDK and App Events for Android and iOS.

What is Facebook SDK Android?

The Facebook SDK for Android gives you access to the following features: Facebook Login — A secure and convenient way for people to log into your app or website by using their Facebook credentials. Sharing — Enable people to post to Facebook from your app. People can share, send a message, and share to stories.


1 Answers

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.

like image 119
sorianiv Avatar answered Sep 29 '22 23:09

sorianiv