Invalid Hash key in facebook app in android

2019-01-24 23:53发布

问题:

I got hash key from below code in android:

try {
PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(),
PackageManager.GET_SIGNATURES);
for (Signature signature : packageInfo.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
}
catch (NameNotFoundException e1) {
Log.e("Name not found", e1.toString());
}
catch (NoSuchAlgorithmException e) {
Log.e("No such an algorithm", e.toString());
}
catch (Exception e){
Log.e("Exception", e.toString());
}

But it was invalid key... Please suggestion how to verify hash key for Facebook login....

回答1:

You are getting the hash-key with debug key... Which may work if you haven't sign the package and running app in debug mode. What you need to do is :

1) Go to the manifest file and add to the application android:debuggable="true".

2) Sign the application, capy and install to your device manually or for use "adb install path_to_apk" from command line.

3) Now run your app and monitor the logcat.

4) You will get printed a new key which will be the matching key with key facebook app is showing in the error msg, The key you have got is now having a = sign in the last.

5) Register this key on facebook developer site

Alternate Trick

You can do one other thing Simply register key which is showing in the error msg "The key hash ### does not match any stored key hashes" Just add the = in the end of the ###. It will be like ###=

you are done!! Hope this will work.



回答2:

Add this function into your class,and call this function in oncreate methode,then generate sign apk and run sign apk in your device and check log-cat, copy generated hash key to facebook developer console.

private void showHashKey()
{
    // Add code to print out the key hash
    try {
        PackageInfo info = getPackageManager().getPackageInfo(
                "com.kisan.kisan",
                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 (PackageManager.NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }

}

Check facebook authentication,it will work fine



回答3:

I think the issue is you might not have updated the keyhash in the facebook developer account. Refer the: https://developers.facebook.com/docs/android/getting-started

Generate the Keyhash using:

keytool -exportcert -alias androiddebugkey -keystore %HOMEPATH%\.android\debug.keystore | openssl sha1 -binary | openssl base64

And update into your facebook devloper site, under Apps tab, General Settings



回答4:

Assign this to a button's on click and get the Key Hash from the logcat.

Button getKeyHash = (Button) findViewById(R.id.button_key_hash);
    getKeyHash.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Key Hash
            try {
                PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(),
                        PackageManager.GET_SIGNATURES);
                for (Signature signature : packageInfo.signatures) {
                    MessageDigest md = MessageDigest.getInstance("SHA");
                    md.update(signature.toByteArray());
                    Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
                }
            } catch (PackageManager.NameNotFoundException e1) {
                Log.e("Name not found", e1.toString());
            } catch (NoSuchAlgorithmException e) {
                Log.e("No such an algorithm", e.toString());
            } catch (Exception e) {
                Log.e("Exception", e.toString());
            }
        }
    });

Now go to your app at https://developers.facebook.com/apps . Then click on settings, add your Key Hash and save.