Key hash for Facebook not working?

#1

Hi,

I’ve just been following this tutorial, and almost like any other, doesn’t really explain how to generate the keyhash. Fortunately, I managed to generate one and using the “release-signing.properties” file in “platforms\android” I’ve managed to get it to output a android-release.apk. Then, with the hash key that was generated, I copied and pasted it into my Facebook App “Key Hashes” textbox and saved it.

In the command prompt (I’m using Windows) I did… “ionic run android --release” which compiled a release copy of the app and installed it on my phone for testing. It works fine but for some reason, the facebook authentication says “Key hash xxxxxxxxxxx= does not match any stored key hashes” which displays a completely different Key Hash in the error message than the one I generated my self.

Any ideas?

Thanks,
Owen

#2

To create a key hash you need to have already created your keystore file for Android, and then just run:

keytool -genkey -v -keystore my-key.keystore -alias my_key -keyalg RSA -keysize 2048 -validity 10000

Making sure to replace my-key.keystore with the path to your keystore file, and my_key with the alias of your keystore.

#3

I created my Key Store from the above command and then retrieved the Key Hash from it by doing;

keytool -exportcert -alias my-key -keystore my-key.keystore | openssl sha1 -binary | openssl base64

I then copied that Key Hash text into my Facebook App settings. Afterwards I edited the “release-signing.properties” to point to the Key Store file created just before, then ran the “ionic run android --release” command which still gave me the error on facebooks authentication of an invalid Key Hash stored.

EDIT: I found the solution. I was using Google’s OpenSSL that seemed to keep giving me a different hash. I came across a StackOverflow thread and read the last comment which seemed to indicate this, and that the solution was to download OpenSSL from here, which solved my problem.

Thanks for the help in the meantime!

2 Likes
Invalid key hash, does not match any stored key hashes
#4

Sorry, my brain must have been sleeping - I meant to post the command for generating a hash, not the keystore, here’s what I use:

keytool -exportcert -alias ALIAS_HERE -keystore path/to/mykey.keystore | openssl sha1 -binary | openssl base64

which is the same as what you just posted - so not sure what’s going wrong. Maybe you could try doing a build command instead of run and install it manually, but it sounds like it should be working. Have you double checked you’re referencing the correct Facebook App in your code as well? That might explain the different key hash.

3 Likes
#5

@Crash
This is how i solved this problem
Downloaded your APK to your PC in java jdk\bin folder

C:\Program Files\Java\jdk1.7.0_79\bin

go to java jdk\bin folder and run cmd then
copy the following command in your cmd

keytool -list -printcert -jarfile apkname.apk

Copy the SHA1 value to your clip board
like this CD:A1:EA:A3:5C:5C:68:FB:FA:0A:6B:E5:5A:72:64:DD:26:8D:44:84
and open http://tomeko.net/online_tools/hex_to_base64.php to convert your SHA1 value to base64. This is what Facebook requires
get
zaHqo1xcaPv6CmvlWnJk3SaNRIQ= as our keyhash.

26 Likes
[TUTORIAL] Here are solutions to few common problems I encountered
Facebook plugin not work in prod version
#6

Hi @nitishrajput01 … Your answer is only workable for me, really thanks a alot…i spend whole day, and now i found what i need.

2 Likes
#7

This worked for me: hash

#8

Thanks! This works for me.

(But instead of copying the APK into the java jdk\bin folder, I just put the bin folder into my Windows %PATH%.)

#9

Wow that was awesome :open_mouth:

#10

Bingo. Worked. Can you please provide more explanation as to why generating new hash key doesnt work?

#11

Great man, it worked for iOS too. Life saving.

#12

Wow. Thanks man. You’re savior.

#13

Thanks dude. Absolute solution!!
I was looking everywhere :wink:

#14

Hi @nitishrajput01 : Thank you. The above solution worked fine for me. I just wanted to know how to handle this case on taking a release build ? Can I follow the same method or should I go with keytool -exportcert -alias ALIAS_HERE -keystore path/to/mykey.keystore | openssl sha1 -binary | openssl base64 and copy paste the hashkey generated to facebook settings ?

#15

like a charm! thanks a lot!

#16

My question is how does the hash key get into the ionic build android and ionic run android ? Where does the hash key have to be located in order for my project to find it. Am I running the keytool from project root and thus would create a keystore in my project root?

Once I have the keystore created, get the key exported into FB is not a problem…its just knowing where the hash is actually being compiled in my project. There seems little documentation on this subtle detail.

My FB login is working…which makes me think somehow my hash key is correctly being compiled and matches the one I copied to FB…however I continue to get a FB error page when trying to actually post something to a users page with showDialog using method: 'feed' …the error page is a generic “sorry, something went wrong” and then returns a “4201 - user cancelled dialog” to my app when the FB error page is closed.

#17

@joshmorony - creating the hash key and exporting/copying it into FB is no problem. But how is the hash key making it into the ionic app? Do we need to specifically add it to the app…is it auto included (how and where)? If the hash is not in the app, then how does it match whats been copied to FB?

Or what am I not understanding here?

#18

Worked for me , Thanks

#19

I finally figured it out. The docs are slim at best on this subject.

  1. By default, ionic/cordova uses the %HOME%.android\debug.keystore hash key during development builds (ie: ionic build android). This is the DEVELOPMENT key you need to extract and add to the FB Android app profile. The default password for this debug.kestore (for both store and key) is android.

  2. When you compile your app for publication, you create your own keystore and reuse that key during the final android compile process (before submitting to store). You will need to add this hash key to the FB Android app profile too. This is the PRODUCTION key.

Both keys should be added to the FB Android app profile…if you ever have to change a key, then the FB app profile must be updated too. Probably best just to add another key versus deleting the old…if you delete the old one and users are on older versions of your app, then deleting the old key will prevent those users from interacting with FB.

#20

thanx bro… really tnax