Invalid Key hash facebook android sdk

2019-04-02 18:24发布

问题:

I'm trying to use Facebook Android SDK to develop a simple app with the Facebook Login Button. But i'm having trouble with Key Hashes. I've created both a debug key and a release key:(in mac)

Debug key:

keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64

keytool -exportcert -alias <RELEASE_KEY_ALIAS> -keystore <RELEASE_KEY_PATH> | openssl sha1 -binary | openssl base64

i also tryed this code also

public static void showHashKey(Context context) {
        try {
            PackageInfo info = context.getPackageManager().getPackageInfo(
                    "com.example.me", PackageManager.GET_SIGNATURES); //Your            package name here
            for (Signature signature : info.signatures) {
                MessageDigest md = MessageDigest.getInstance("SHA");
                md.update(signature.toByteArray());
                Log.i("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
                }
        } catch (NameNotFoundException e) {
        } catch (NoSuchAlgorithmException e) {
        }
    }

And then i copied this key hashes in the Facebook Developers page.when i run my app with eclipse then working.but When i export the apk and copy it into the device it does not working. also i have another problem.if my divice has installed Facebook Application then also not working. how i can solve my problem?

回答1:

When you get Invalid key hash, it also shows the correct key hash and says "The keyhash XXXXX didnot match with any of the stored keyhashes" . so copy that displayed keyhash and append "=" at the end as XXXXX= and add it in the key hash section. Hope it works....

this might also work

public static void showHashKey(Context context) {
    try {
        PackageInfo info = context.getPackageManager().getPackageInfo(
                "com.example.me", PackageManager.GET_SIGNATURES); //Your package name here
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.i("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
            }
    } catch (NameNotFoundException e) {
    } catch (NoSuchAlgorithmException e) {
    }
}