Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Facebook SDK 4.5.0 Login-Logout Issue Invalid Key Hash Error, While Trying to Login Again

I implemented Facebook login button as described in https://developers.facebook.com/docs/facebook-login/android developer guide with profile and email read permissions.

When i press login button, Facebook app opens up and then I can log in and can get user data from Facebook. After this point, Facebook button turns to Log out button automatically. And when it is pressed, it logs out. So far, it works well.

Once Facebook log out done in my app side, and want to re-login with Facebook button, Facebook fails with key hash error. If I go to account settings in Facebook app, and remove my app from list then re-login returns success.

I also tried the solution here Android Facebook app logout issue but it didn't work either. To clear, I use this code (found shared pref name in AccessTokenCache class):

   SharedPreferences fbSharedPreferences = this.getSharedPreferences("com.facebook.AccessTokenManager.SharedPreferences", 0);
   if (fbSharedPreferences != null) {
       fbSharedPreferences.edit().clear().commit();
   }

I'm using Facebook SDK 4.5. I'm testing with a real Facebook account. My app keys and hashes are set in Facebook app settings.

P.S. Question title is influenced from Facebook Login-Logout Issue Invalid Key Hash Error, While Trying to Login Again (which does not have a solution).

like image 597
enver Avatar asked Oct 19 '22 00:10

enver


1 Answers

I was having the same issue, you need to delete app from facebook app and then logout. Following function will do the trick.

public void disconnectFromFacebook() {

    if (AccessToken.getCurrentAccessToken() == null) {
        return; // already logged out
    }

    new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/permissions/", null, HttpMethod.DELETE, new GraphRequest
            .Callback() {
        @Override
        public void onCompleted(GraphResponse graphResponse) {

            LoginManager.getInstance().logOut();

        }
    }).executeAsync();
}
like image 179
Abhishek Patidar Avatar answered Oct 22 '22 00:10

Abhishek Patidar