Can't relogin with Facebook credentials after logout

馋奶兔 提交于 2019-12-04 13:33:27

问题


I have an Android application that allow users to login with their Facebook credentials. Once they logout for the first time, they can't login again. After clearing the application's permission from the Applications page in user settings on Facebook's website, logging in works correctly.

I use a LoginButton to login.

Activities that need to be able to access the login information, extend this AuthActivity Activity.

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 look like this. I stripped of all Google+ login related stuff and everything that doesn't have anything to do with the login process, like saving the user information with my application preferences.

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:


I had the same problem, even when running Facebook's sample apps. I solved this by providing my default signing key to Facebook: both in my Developer Settings for the Sample Apps at https://developers.facebook.com/settings/developer/sample-app/ and then in your Apps settings in the Android platform.

Facebook suggests an easy way to get at your default key which can be found under Troubleshooting at https://developers.facebook.com/docs/android/getting-started. The code for doing so when running the Hello Facebook example app is provided below.

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

Note: When you're publishing apps, you shouldn't be using the default key and be generating and signing apps with your own.



来源:https://stackoverflow.com/questions/24149499/cant-relogin-with-facebook-credentials-after-logout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!