42

I'm building an app in which users can log in with Facebook.

I've created the hash keys like following:

try {
         PackageInfo info = getPackageManager().getPackageInfo(
         "com.app.package",
         PackageManager.GET_SIGNATURES);
         for (Signature signature : info.signatures) {
         MessageDigest md = MessageDigest.getInstance("SHA");
         md.update(signature.toByteArray());
         Log.d("KeyHash", "KeyHash:"+ Base64.encodeToString(md.digest(),
         Base64.DEFAULT));
         Toast.makeText(getApplicationContext(), Base64.encodeToString(md.digest(),
                 Base64.DEFAULT), Toast.LENGTH_LONG).show();
         }
         } catch (NameNotFoundException e) {

         } catch (NoSuchAlgorithmException e) {

         }

In debug mode, everything works well.

When I export the project for release, it gives this error:

"Invalid key hash. The key hash ****************** does not match any stored key hashes"

I paste the key printed in the Facebook Developer dashboard, but the application still gives me that error.

The complete package of my actvity is "com.app.package.views" and I tried to use this package (as Google Play Package Name) in the dashboard, but nothing changed.

How can I solve it? How can I generate the right release key hash?

Irshu
  • 7,818
  • 8
  • 49
  • 61
MikeKeepsOnShine
  • 1,664
  • 4
  • 23
  • 35

13 Answers13

55

You followed the steps that facebook provides for the creation of a login application?

You need a 'Production keyhash' obtained starting your release keystore:

From comand line:

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

And add this key on facebook app page options.

More information: https://developers.facebook.com/docs/android/getting-started/

Paul Peelen
  • 9,822
  • 15
  • 84
  • 165
Abhi
  • 3,012
  • 21
  • 14
  • 1
    what is mean by – Muhammad Younas Jul 12 '16 at 14:01
  • 1
    I think Its just a Alias or name to be used when generating Hash keys – Akash Bisariya Dec 15 '16 at 08:19
  • 4
    can you tell me where I run this command, because when I run it it tell me 'keytool' is not recognized as an internal or external command, operable program or batch file. – Tefa Mar 07 '17 at 01:01
  • 1
    I've done the same, tested on the app with apk-release file. But didn't work after uploaded it to the play store ?! – TomSawyer Sep 03 '17 at 09:50
  • It worked for sometimes, again its not working. Showing the message like invalid key hash. – Akshay kumar Apr 14 '18 at 06:41
  • 1
    Here is the name of alias which you have created while submitting your app with the release keystore file and , is the path of release keystore from which you have uploaded your app on play store. – B.shruti Oct 04 '18 at 07:31
  • **Run the following command in a command prompt in the Java SDK folder** .e.g: `C:\Program Files\Java\jre1.8.0_201\bin>...` @Tefa – Adarsh Vijayan P Feb 14 '19 at 06:39
  • @Abhi I have added the new key(as told above) hash to settings —> basic –> key hashes. But still Im getting the same error(only on the release). Interesting part is the invalid key hash which is shown in the error is not even mentioned by me in the settings —> basic –> key hashes and i dnt know where it came from – Shikhar Mar 18 '19 at 18:59
  • @ShikharJaiswal add this keyHash in your app account on facebook, pick from error it is your release keystore keyHash using this you uploaded your app – Dinesh May 07 '19 at 06:32
  • Does it still work after publishing on Google Play? – c-an Jun 01 '20 at 04:26
  • this not actual answer anymore when you use Google Play Signing, this is just a upload keystore and not release one! – user924 Nov 12 '21 at 12:20
50

I find a solution. for MAC

Use this one to get YOUR_RELEASE_KEY_ALIAS:

keytool -list -keystore /Users/***/Documents/keystore/***.jks

and this one to get your release keyhash:

keytool -exportcert -alias YOUR_RELEASE_KEY_ALIAS -keystore /Users/***/Documents/keystore/***.jks | openssl sha1 -binary | openssl base64

It works for me.

Rahim Rahimov
  • 1,307
  • 14
  • 24
37

For future reference, if you already have your app on Play Store you can this:

  1. Go to Release Management

  2. select App Signing in Release Management

  3. You can see SHA1 key in hex format App signing certificate.

  4. Copy the SHA1 in hex format and convert it in to base64 format, you can use this link http://tomeko.net/online_tools/hex_to_base64.php to do that without the SHA1: part of the hex.

  5. Go to Facebook developer console and add the key(after convert to base 64) in the settings —> basic –> key hashes.

Nadhir Falta
  • 4,555
  • 1
  • 17
  • 42
  • I have added the new converted key hash to settings —> basic –> key hashes. But still Im getting the same error(only on the release). Interesting part is the invalid key hash which is shown in the error is not even mentioned by me in the settings —> basic –> key hashes and i dnt know where it came from – Shikhar Mar 18 '19 at 10:00
  • Thanks for the working link. :) Just wanted to add: It was different when I generated it through Command Prompt as described in FB Documentation. – Rohit Sharma Aug 20 '20 at 14:20
  • I'm using the signing key from Google and couldn't use other methods. This method works like a charm for me. – Anky An Mar 01 '22 at 11:59
34

The simplest solution.

1) Sign your Apk.

2) Connect your device to machine and Install signed apk on real device.

3) When facebook login is pressed, you will get an error saying "Invalid key hash. The key hash "xxx" does not match any stored key. ..." on your logcat.

4)Copy the logcat Hash Key and put this key to developers.facebook.com/apps/104...../settings/

Bharat Hangarge
  • 564
  • 5
  • 12
9

We need to replace the word "openssl" by the path of one file inside the openssl structure.

So, My CMD command is:

C:\Program Files\Java\jre1.8.0_45\bin>keytool -exportcert -alias Informatheus -keystore C:\Users\Atendimento\Dropbox\AndroidKeystore\Keystore | C:\Users\Atendimento\Desktop\openssl\bin\openssl sha1 -binary | C:\Users\Atendimento\Desktop\openssl\bin\openssl base64

It worked.

Informatheus
  • 1,015
  • 1
  • 8
  • 20
5

// add this method to your first activity and open log and search for Base64 tag this is the Hashkey i hoop it help

public void getHashkey(){
    try {
        PackageInfo info = getPackageManager().getPackageInfo(getApplicationContext().getPackageName(), PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());

            Log.i("Base64", Base64.encodeToString(md.digest(),Base64.NO_WRAP));
        }
    } catch (PackageManager.NameNotFoundException e) {
        Log.d("Name not found", e.getMessage(), e);

    } catch (NoSuchAlgorithmException e) {
        Log.d("Error", e.getMessage(), e);
    }
}
eng mohamed emam
  • 509
  • 6
  • 11
3

The simplest way to get hash key released apk is : Get SHA1 key of released apk using following command:

keytool -list -v -keystore keystore_path.jks -alias keystoreAlias

Thn you will get SHA1 key. Copy that key and generate hash key using following site:

Link to get hash key

You will get Output (base64): copy it and use where you want..

Pratibha Sarode
  • 1,751
  • 16
  • 17
2
  1. First open the command prompt (Windows + R)

  2. cd C:\Program Files\Java\jre1.8.0_172\bin

  3. Download openssl from HERE

  4. Create a folder named OpenSSL in C:/ drive

  5. Export the downloaded zip file in OpenSSL folder

  6. Generate the command as:-

    keytool -exportcert -alias YOUR_ALIAS -keystore "YOUR_KEYSTORE_PATH" | "C:\OpenSSL\bin\openssl" sha1 -binary | "C:\OpenSSL\bin\openssl" base64

  7. Paste the command in command prompt and press enter

  8. Enter password

  9. Here you can see the key :- "1skdhyjsgd56whdjddV+vCLE="

Vikram Kodag
  • 375
  • 3
  • 5
2

July 2021: Find key in Google Console, if your app is already released

  1. Google Play Consolez -> left menu Release -> Setup -> App Integrity -> Upload key certificate -> SHA-1 certificate fingerprint

  2. Convert HEX to base64: https://base64.guru/converter/encode/hex

  3. Add key in Facebook: Add your development and release key hashes

rusakovic
  • 99
  • 5
  • Thanks so god damn much for this solution. Finally I can log in with Facebook. I've been going nuts over the last 8 hours trying to solve this. – Weblurk Jan 15 '22 at 20:44
1

Facebook SDK uses two different keys, one is Debug key which you can use during your development phase and other is Release key which is used once you create a signed application package. Here is a link about how to create debug and release keys.

Developer.Facebook

Also check out this SO post.

Community
  • 1
  • 1
MajorGeek
  • 485
  • 1
  • 13
  • 30
-1

This one is the easiest way i found so far for generating key hash!

try
{
  PackageInfo info = getPackageManager().getPackageInfo(
      "Your Package Name",
      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) {

}
Rahul
  • 1
  • 4
-1

You have to generate two Key Hashes. The first using the above command.

And once your app is in play store go to

Configuration -> app integrity 

and grab the SHA1 that google generated and go to this site :

www.fbkeyhash.com 

Paste the SHA1 and generate the second Key Hash.

Both of them need to be saved in the Facebook console in order for the live and debug version of your APK to work.

Biggg Jimmm
  • 67
  • 2
  • 12
-3
keytool -exportcert -alias aliasName -keystore C:\my_release_keystroke_info.jks | openssl sha1 -binary | openssl base64
Wtower
  • 17,145
  • 11
  • 98
  • 72
Nitin Anand
  • 737
  • 6
  • 9
  • 1
    Could you please add an explanation on what this does and how it helps? – Wtower Jul 04 '16 at 05:24
  • This appears to be just a repeat of the existing answers - [From Review](http://stackoverflow.com/review/low-quality-posts/12894956). – Pang Jul 05 '16 at 00:50