46

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
  • 8,248
  • 8
  • 53
  • 65
MikeKeepsOnShine
  • 1,730
  • 4
  • 23
  • 35

12 Answers12

52

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,347
  • 15
  • 24
44

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
  • 5,047
  • 2
  • 21
  • 43
  • 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
  • 571
  • 5
  • 13
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,025
  • 1
  • 9
  • 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
  • 549
  • 6
  • 13
4

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
  • 126
  • 6
  • 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
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,819
  • 17
  • 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
  • 485
  • 5
  • 6
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
  • 495
  • 1
  • 13
  • 31
-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
  • 18,848
  • 11
  • 103
  • 80
Nitin Anand
  • 795
  • 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