0

I know its very old and there are many questions with similar problem. I have already seen different answers for it but still the problem is coming in my application.

I am working on Facebook login in app. Before uploading to Google Play it is working perfectly locally. But after publishing on Google Play, it is giving an error of INVALID KEY HASH.

I have generated a key hash on Android Studio using the code given below and saved it on the Facebook Developer Console. In the Facebook Developer Console, I have also put the key hash which comes on the Error message on the App but still it is giving me the same error.

PackageInfo info;
        try {
            info = getPackageManager().getPackageInfo("i.am.peace.by.murgency", 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());
        }

Through terminal with this statement:

keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64
halfer
  • 19,824
  • 17
  • 99
  • 186
Usman Khan
  • 3,739
  • 6
  • 41
  • 89
  • Are you having 2 key hashes on your Facebook developer console ? I mean a release key and a debug key or just a single debug key hash ?? – Asutosh Panda Jan 10 '18 at 17:08
  • @Zoe, if you read my question, clearly I mentioned there that I have read many messages but same problem I am getting. – Usman Khan Jan 10 '18 at 17:10
  • @AsutoshPanda thanks for your msg. While debugging I put Hashkey from my code to facebook. After that I haven't put any key there. Only I put Error Message key hash there. Can you plx hel me to fix it. – Usman Khan Jan 10 '18 at 17:11
  • Check my answer. You have to add a release key too. – Asutosh Panda Jan 10 '18 at 17:25

2 Answers2

1

Look, you generate KEY HASH with debug key ~/.android/debug.keystore but you need to set here your key which you using for publish on Google Play ~/.android/release.keystore

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

and generated KEY HASH add to facebook developer console

Step by step: Go to https://developers.facebook.com and open Settings->Basic->Android->Key Hashes and copy paste here you KEY HASH that you generated with keytool, BUT ONLY using you release key, NOT debug (see command above)

Igor Kostenko
  • 2,754
  • 7
  • 30
  • 57
  • @Ihot thanks for your message here. Can you please give me little more explanation for it. – Usman Khan Jan 10 '18 at 17:12
  • I did above one and saved it there, but still it is giving me the same error. – Usman Khan Jan 10 '18 at 17:14
  • You need to sign app and generate key hash for facebook with same keystore. And then add it to facebook console – Igor Kostenko Jan 10 '18 at 17:21
  • 1
    I was doing a smelling mistake. Thanks it works :) – Usman Khan Jan 10 '18 at 17:21
  • Hi Igor Kostenko, I am having the same issue. Actually my app login facebook OK. So when I upgrade version android to android x, I login failed. It show invlaid hash key. I followed the facebook SDK tutorial, the hash key I generated from my app is the same as the setting in facebook. I don't know what it is wrong :( – vannguyen Oct 06 '19 at 05:54
1

To use Facebook SDK in a release APK, you also need to add a release key hash along with your debug key hash in the Facebook developer console.

The key that you have generated from code and from terminal is a debug key. You need to generate a release key as mentioned below and add to your Facebook developer console.

If you are on a mac, paste this on a Terminal to generate a release key -

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

If you are on Windows, in the command prompt, go to JDK folder and paste this to generate a release key —

keytool -exportcert -alias androiddebugkey -keystore %HOMEPATH%\.android\debug.keystore | openssl sha1 -binary | openssl base64

Please note that, androiddebugkey above is the alias name. Make sure that the alias name when generating a release key is the same as the alias name when you generated a debug key.

More info is here - https://developers.facebook.com/docs/android/getting-started#release-key-hash

Asutosh Panda
  • 1,463
  • 2
  • 13
  • 25