注销后无法使用Facebook凭据重新登录

时间:2014-06-10 19:36:54

标签: java android facebook facebook-login facebook-android-sdk

我有一个Android应用程序,允许用户使用他们的Facebook凭据登录。一旦他们第一次注销,他们就无法再次登录。在Facebook的网站上Applications page in user settings清除了应用程序的许可后,登录工作正常。

我使用LoginButton登录。

需要能够访问登录信息的活动,扩展此AuthActivity活动。

public class AuthActivity extends Activity {
    private AuthUtils authUtils;

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

        authUtils = new AuthUtils(this);
        authUtils.onCreate(savedInstanceState);
    }

    public AuthUtils getAuthUtils() {
        return authUtils;
    }

    @Override
    public void onStart() {
        super.onStart();
        authUtils.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
        authUtils.onStop();
    }

    @Override
    public void onResume() {
        super.onResume();
        authUtils.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        authUtils.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        authUtils.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        authUtils.onSaveInstanceState(outState);
    }

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

AuthUtils看起来像这样。我删除了所有与Google+登录相关的内容以及与登录过程无关的所有内容,例如使用我的应用偏好设置保存用户信息。

public class AuthUtils implements ConnectionCallbacks, OnConnectionFailedListener {
    private final Activity activityContext;
    private UiLifecycleHelper facebookUiHelper;

    public AuthUtils(Activity context) {
        this.activityContext = context;

        facebookUiHelper = new UiLifecycleHelper(context, facebookStatusCallback);
    }

    public void signInFacebook() {
        LoginButton facebookLoginButton = new LoginButton(activityContext);
        facebookLoginButton.setReadPermissions(Arrays.asList("email"));
        facebookLoginButton.performClick();
    }

    public void signOutFacebook() {
        Session facebookSession = Session.getActiveSession();
        if(facebookSession != null) {
            facebookSession.closeAndClearTokenInformation();
        }
    }

    private StatusCallback facebookStatusCallback = new StatusCallback() {
        @Override
        public void call(final Session session, SessionState state, Exception exception) {
            if(state.isOpened()) {
                Request.newMeRequest(session, new GraphUserCallback() {
                    @Override
                    public void onCompleted(GraphUser user, Response response) {
                        String email = "";

                        if(user.asMap().containsKey("email")) {
                            email = user.getProperty("email").toString();
                        } else {
                            // ... not related to login
                        }

                        // Some actions here, not related to login.
                    }
                });
            } else if(state.isClosed()) {
                // ... not related to login
            }
        }
    };

    public void onCreate(Bundle savedInstanceState) {
        facebookUiHelper.onCreate(savedInstanceState);
    }

    public void onResume() {
        facebookUiHelper.onResume();
    }

    public void onPause() {
        facebookUiHelper.onPause();
    }

    public void onDestroy() {
        facebookUiHelper.onDestroy();
    }

    public void onSaveInstanceState(Bundle outState) {
        facebookUiHelper.onSaveInstanceState(outState);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        facebookUiHelper.onActivityResult(requestCode, resultCode, data);
    }
}

1 个答案:

答案 0 :(得分:0)

即使在运行Facebook的示例应用程序时,我也遇到了同样的问题。我通过向Facebook提供我的默认签名密钥解决了这个问题:我在https://developers.facebook.com/settings/developer/sample-app/的示例应用程序的开发人员设置中,然后在Android平台的应用程序设置中。

Facebook提供了一种简单的方法来获取您的默认密钥,可以在https://developers.facebook.com/docs/android/getting-started的故障排除下找到。运行Hello Facebook示例应用程序时执行此操作的代码如下所示。

try {
    PackageInfo info = getPackageManager().getPackageInfo(
        "com.facebook.samples.hellofacebook", 
        PackageManager.GET_SIGNATURES);

    for (Signature signature : info.signatures) {
        MessageDigest md = MessageDigest.getInstance("SHA");
        md.update(signature.toByteArray());
        Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
    }
} catch (NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}

注意:当您发布应用时,您不应使用默认密钥,而是使用自己的应用生成和签署应用。