Search code examples
androidhashfacebook-sdk-4.0

Facebook android app error : Invalid key hash


I have imported the Facebook SDK to my android project, first time I logged in to Facebook from my android application it worked, but the second time I got this 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/..

I also tried to enter the key they provided in this error but it didn't work.

EDIT: I have used the cmd command to which it generates a key and asks for the password which is "android"


Solution

  • As I understand you've got your key hash, but still I'll put here the code for getting it in the console.

    PackageInfo info;
    try {
        info = getPackageManager().getPackageInfo("com.your.project.package", PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md;
            md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            String something = new String(Base64.encode(md.digest(), 0));
            //String something = new String(Base64.encodeBytes(md.digest()));
            Log.e("hash key", something);
        }
    } 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());
    }
    

    Where "com.your.project.package" is the package of your project =)

    Next thing, dont delete previous key hash, they don't conflict I think. For example I have 2 keyhashes in my facebook app.

    And the latest thing, and I think this is the problem. Go to the Status&Review of your app at the developers.facebook. And switch your application to public. public access to app