2

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"

MuhammadNe
  • 614
  • 3
  • 9
  • 22

6 Answers6

9

Keyhashes differs with .apk files generated from different systems. So best solution is - when you get error

"Invalid key hash. The key hash "<KEYHASH>" does not match any..."

then write or Copy that "<KEYHASH>" and Put that in facebook developer page by go to apps-> settings-> android. If doesn't work then put "=" after keyhash you copied from error and try again. And don't forget to live your project.

NehaK
  • 2,128
  • 1
  • 11
  • 29
  • KeyHash of error and deveper facebook keyhash are same or not? – NehaK Jun 19 '15 at 09:27
  • It will definitely work.. Try to copy that keyhash that is showing in error and put it to developer. If doesn't work then put "=" after it. – NehaK Jun 19 '15 at 09:28
  • i removed it from the key hashes, and then added it again in the facebook developers page -> my app -> settings -> under the android block inside the key hashes and saved changes, is there anything missing ? because it still gives me the same error – MuhammadNe Jun 19 '15 at 09:33
  • check there should be "=" at the end of keyhash. – NehaK Jun 19 '15 at 09:34
  • bh+kxfeetr487KNdM4dEcl0894A= thats the key hash – MuhammadNe Jun 19 '15 at 09:35
  • its from the facebook page that opens inside the android app – MuhammadNe Jun 19 '15 at 09:39
  • Rematch it to facebook developer page's keyhash you added. it should work. Because i got same error and solved by this solution. – NehaK Jun 19 '15 at 09:40
  • 1
    Yes this is the solution, you need to add it to keyHash page with = at the end,it defiantly work,I faced same problem and this solve that error. – Divya Jain Jun 19 '15 at 10:07
  • 2
    Now I know why this happens every time we set up a new development machine. Thank you! – Rjak Oct 02 '16 at 15:39
6

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

Max Zavernutiy
  • 1,447
  • 1
  • 16
  • 27
  • do i add this code inside the facebook activity? inside it i have the register callback, should i put it there or where exactly? sorry for asking this question – MuhammadNe Jun 19 '15 at 09:48
  • @user3245316 you can put it everywhere. It just logs the key hash of your app to the console with tag "key hash". You can put it in the main activity `onCreate()` method. Have you made your application public, as I suggested on the screenshot? Because I think I had the same problem, until I didn't make my app public =) – Max Zavernutiy Jun 19 '15 at 09:52
  • You won't believe what was the problem, i saw a letter and thought it was a small L, but it turned out to be a capital i, i saw that one after printing the hash inside the console, thank you – MuhammadNe Jun 19 '15 at 10:10
6

You should get new key;

Step 1. If you dont have OpenSSL lib, Download here

Step 2. And Extract it. create a folder- OpenSSL in C:/ and copy the extracted code here.

Step 3. And open cmd and go C:\Program Files\Java\jdk1.7.0_79\bin

Step 4. And execute this command -

keytool -exportcert -alias androiddebugkey -keystore %HOMEPATH%\.android\debug.keystore | "C:\OpenSSL\bin\openssl" sha1 -binary |"C:\OpenSSL\bin\openssl" base64

Step 5. Get key and go developer facebook website

Step6. Add key to your project.

That's it.

Berat Eyüboğlu
  • 1,343
  • 2
  • 15
  • 31
5

Add the key Provided in your app on

http://developers.facebook.com/apps/

Additionally, in last add "=" symbol

ex. you are getting 2jmj7l5rSw0yVb/vlWAYkK/Y than in Facebook page, add 2jmj7l5rSw0yVb/vlWAYkK/Y= That Last "=" sign is important.

I spent an hour generating the keyhashes, than tried this trick.

NehaK
  • 2,128
  • 1
  • 11
  • 29
Neal Ahluvalia
  • 1,528
  • 1
  • 9
  • 20
3

This worked for me.

Copy the APK to your PC in Program Files\java\jdkX.X.X_XXX\bin folder

In my case it's C:\Program Files\Java\jdk1.8.0_191\bin

Open CMD in this directory and type the following

keytool -list -printcert -jarfile YOUR_APK_NAME.apk

Copy the SHA1 value to your clipboard It will be something like this: 79:D0:E6:80:4E:28:1E:D1:88:28:CB:D7:E6:BE:2E:0C:FB:24:98:52

Then go to http://tomeko.net/online_tools/hex_to_base64.php to convert your SHA1 value to base64.

This is what Facebook requires get the generated hash " ********************= " and copy the key hash to your Facebook developers app settings.

Bhavik Nathani
  • 357
  • 4
  • 12
  • 1
    This online tool is the only way I was able to get the correct key hash value for Facebook Login. Other tools including openssl for windows gave me wrong hashes. Thank you! – Vit Veres May 21 '20 at 07:30
0

there are two possible solutions that might help, each one of these got it done for me in different occassions so here we go.

(assumming that you typed the hash key that the app told you to and didn't work you)

  1. You should delete de debug.keystore file located usually in "C:\Users\yourUserName.android", then recompile the project, and get the new hash key with thes new debug.keyhash (as stated here ). Then these new keyhash should work.

2.These is a little more tricky. Maybe the reason why your facebook developer dashboard key hash doesn't work even if you typed it, is because your facebook app ID has changed by the moment you built the apk (this happened to me because i had previous versions of my app and it was giving me the past id, even though i had it specified in config.xml). So, in my case i have a cordova project so i went to "C:your_CordovaProject\platforms\android\res\values\facebookconnect.xml" and open the file, here you gotta check if the fb_app_id is the same that you have in your facebook dashboard. if it's not (it wasn't in my case) what you have to do is --Remove your facebook plugin, --add it again and --specify your app id and name one more time like this:

cordova plugin add cordova-plugin-facebook4 --save --variable APP_ID="123456789" --variable APP_NAME="myApplication"

then i did the hash key process again and it worked. Hope it helps

Community
  • 1
  • 1