0

I am integrating Facebook Login into my app. Logging in works, but when I logoff and try to log back in again, I get the following error: "Invalid key hash. The key hash "..." does not match any stored key hashes. Configure your app key hashes at http://developers.facebook.com/apps/ ..."

The error only occurs after I try to login for a second time. Below is the code I used. Where am I going wrong?

public class LoginActivity extends AppCompatActivity {
    private CallbackManager mCallbackManager;
    private LoginButton loginButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(this.getApplicationContext());

        setContentView(R.layout.activity_login);

        mCallbackManager = CallbackManager.Factory.create();

        LoginManager.getInstance().registerCallback(mCallbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        Log.i("LOGINFACEBOOK", "onSuccess? " + loginResult.toString());
                    }

                    @Override
                    public void onCancel() {
                        Log.i("LOGINFACEBOOK", "onCancel");
                    }

                    @Override
                    public void onError(FacebookException exception) {
                        Log.i("LOGINFACEBOOK", "onError? " + exception.toString());
                    }
                });

        LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
        loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                Log.i("LOGINFACEBOOK", "loginButton onSuccess? " + loginResult.toString());
            }

            @Override
            public void onCancel() {
                Log.i("LOGINFACEBOOK", "loginButton onCancel");
            }

            @Override
            public void onError(FacebookException error) {
                Log.i("LOGINFACEBOOK", "loginButton onError? " + error.toString());
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mCallbackManager.onActivityResult(requestCode, resultCode, data);
    }
}
fionaredmond
  • 639
  • 7
  • 15
  • This standard implementation of facebook. just note down OR Log/Alert the hashkey displayed in toast/error & put it in your web facebook app hashkey. – Ajinkya Jan 06 '16 at 18:28
  • Did not work. I put the hash that appears in the error in the APP on Facebook but still see the same error . The strange thing is that it works the first time you realize login. – Danilo Andrade Jan 07 '16 at 10:38
  • One more information. Uninstalled Facebook APP to test and functioned normally without it. When I click the Facebook loginButton it opens the webview and functions normally. – Danilo Andrade Jan 07 '16 at 11:48
  • tell your sdk version – Aniruddha K.M Feb 04 '16 at 09:21

1 Answers1

0

Try to follow these intructions, the user Who posted this question had your problem.

Summary of the solution:

Follow this steps:

Paste the following code in oncreate().

try {
    PackageInfo info = getPackageManager().getPackageInfo(
            "com.example.packagename", 
            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) {

}

Modify "com.example.packagename" with your package name in the above coding without fail(You may found your package name in Android Manifest file).

Run your application. Go to the activity where you pasted the above code. In the logcat search for "KeyHash". You may found a key hash. Copy the key hash and go to Facebook application dashboard page. Go to settings and enter the details.

Once you finished the above step. Relaunch the app again you may now log into the facebook. For more details about key hash check the link

If you add a wrong information in the settings page means it will give some error. so use the correct information there. And also if public(other than you) need to use your application means you need to enable the permission(change "yes" in the "Status & Review" next to setting).

Community
  • 1
  • 1
silviagreen
  • 1,679
  • 1
  • 18
  • 39