在Android上使用Facebook的SDK给"在不同的Facebook用户登录的用户和QUOT。错误

问题描述:

我升级我使用Facebook的SDK到最新版本。下面code是preTY备受线从Facebook的自己的示例解除线,可以在这里找到:的 https://developers.facebook.com/docs/facebook-login/android/v2.3

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

问题是,当用户在自己的手机上的Facebook应用已经登录这不起作用。在这种情况下,这给错误登录为不同的用户的Facebook用户。

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.

我倒觉得既然已经有设备上的一个活跃的Facebook帐户,一个新的不能创建。但是,如果是这样的话,我应该得到用户的ID,并从现有帐户标记?我不能似乎能够找到这个问题的任何提及Facebook的文档页面。

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.

如果你想与现在登录Facebook上的应用程序,你还有一个令牌有效期为previous用户的用户登录,你可以检测到错误并在FacebookCallback这样退出:

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

然后就可以再次登录使用LoginManager。

Then you can log in again using the LoginManager.