Facebook login on Android - works only if Facebook app is not installed !?

If there is no facebook app installed, the login works.

But if the facebook app is installed, getting this error:

12-16 01:38:03.497: W/fb4a(:<default>):BlueServiceQueue(23915): com.facebook.http.protocol.ApiException: Key hash xxxxxxxxxxxxxxxxxxxxx does not match any stored key hashes.
 

Tried to add the key hash from the error into facebook page, but that didn’t solved it.

I have searched the forum and the net, seeing that people had similar trouble, but found no solution.

What is weird, why does it work with the facebook web login (without the facebook app) !?

Let me drop back and get really basic with this.

A keystore is a file that contains information that can be used to encrypt your app to keep it from being easily stolen.  It is referred to as “digitally signing” your app.  It gives it a signature that makes sure you are actually you.   This keystore is a file, stored somewhere on your computer.

There are two types:  A debug keystore, used for testing.   A debug keystore cannot be uploaded to a store.  Then you have a release keystore that is used for stores.   The debug keystore is named debugkeystore or something obvious.  The release keystore you can name whatever on earth you want it to be.   Each keystore also has an “Alias Name” (that honestly I don’t know what it’s used for, just that it has to match what you set up) that you have to define.

A key hash involves taking that keystore file and running it through a process that generates a short, unique and non-reversible “hash” of the keystore.  This is the value that Facebook wants.

Problem  #1.  The command Facebook would have you run is for a debug keystore that is in the Android SDK file somewhere.   Corona SDK, when you build with a debug keystore uses one located in the Corona SDK folder.  This debug keystore is different than the the Facebook’s instructions gives you.   Facebook would have you do:

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

But that’s not the right keystore.  You need to execute:

cd /Applications/CoronaSDK/Resource\ Library/Android/ keytool -exportcert -alias androiddebugkey -keystore debug.keystore | openssl sha1 -binary | openssl base64

Then if you build using a debug keystore in Corona you will have the right value.   But…

You cannot deliver this app to a store for other people to get, so you need to use a release keystore.  I have no idea where on your hard drive you have it saved (if you have one).  I have no idea what alias you gave it.  But you should use the “cd” command and change to the folder where it’s stored then execute the keytool command above substituting the alias and keystore values for what you already used.  Then you make sure to build against it.  Whatever keyhash you build with is what you need to tell facebook.

Now if the log file has the keyhash you need, you can just copy it (It may need an = sign on the end).   For more details on this, we wrote an extensive tutorial for facebook authentication:

http://coronalabs.com/blog/2013/07/30/understanding-facebook-authentication/

Rob

Hi Rob,

I really appreciate your help and time you spent on writing and explaining this.

BUT I have read everything about generating hash key I found, I did it several times.

I generated the hash with my production keystore, everything went well (asking for password etc.).

I entered that hashkey into facebook site.

As I wrote above, the login WORKS if the facebook app is NOT installed.

If it is installed, I am getting the above error.

Also, what seems to have helped some people, doesn’t help in my case - entering the hash key reported in the error in the facebook dashboard.

I have searched the net and found that you have to replace the _ with / in this error-hash - didn’t help.

So am I really clueless right now.

Any additional help would be really appreciated.

Regards,

Damir.

After hours and hours of trying everything possible I finally found a solution!

First, for the facebook web-login (no native app installed) the hash DOES NOT MATTER, it works even with a wrong one.

Second, the keytool command obviously generated a totally wrong hash for my production keystore.

So I found this peace of code on the net and run it on one of my native games:

try { PackageInfo info = getPackageManager().getPackageInfo("com.package.mypackage", PackageManager.GET\_SIGNATURES); for (Signature signature : info.signatures) { MessageDigest md = MessageDigest.getInstance("SHA"); md.update(signature.toByteArray()); String sign=Base64.encodeToString(md.digest(), Base64.DEFAULT); Log.e("MY KEY HASH:", sign); Toast.makeText(getApplicationContext(),sign, Toast.LENGTH\_LONG).show(); } } catch (NameNotFoundException e) { } catch (NoSuchAlgorithmException e) { } &nbsp;

Guess what ?

Got almost the same hash as the one in the log, except _ is replaced with / and - with +    !!!

Let me drop back and get really basic with this.

A keystore is a file that contains information that can be used to encrypt your app to keep it from being easily stolen.  It is referred to as “digitally signing” your app.  It gives it a signature that makes sure you are actually you.   This keystore is a file, stored somewhere on your computer.

There are two types:  A debug keystore, used for testing.   A debug keystore cannot be uploaded to a store.  Then you have a release keystore that is used for stores.   The debug keystore is named debugkeystore or something obvious.  The release keystore you can name whatever on earth you want it to be.   Each keystore also has an “Alias Name” (that honestly I don’t know what it’s used for, just that it has to match what you set up) that you have to define.

A key hash involves taking that keystore file and running it through a process that generates a short, unique and non-reversible “hash” of the keystore.  This is the value that Facebook wants.

Problem  #1.  The command Facebook would have you run is for a debug keystore that is in the Android SDK file somewhere.   Corona SDK, when you build with a debug keystore uses one located in the Corona SDK folder.  This debug keystore is different than the the Facebook’s instructions gives you.   Facebook would have you do:

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

But that’s not the right keystore.  You need to execute:

cd /Applications/CoronaSDK/Resource\ Library/Android/ keytool -exportcert -alias androiddebugkey -keystore debug.keystore | openssl sha1 -binary | openssl base64

Then if you build using a debug keystore in Corona you will have the right value.   But…

You cannot deliver this app to a store for other people to get, so you need to use a release keystore.  I have no idea where on your hard drive you have it saved (if you have one).  I have no idea what alias you gave it.  But you should use the “cd” command and change to the folder where it’s stored then execute the keytool command above substituting the alias and keystore values for what you already used.  Then you make sure to build against it.  Whatever keyhash you build with is what you need to tell facebook.

Now if the log file has the keyhash you need, you can just copy it (It may need an = sign on the end).   For more details on this, we wrote an extensive tutorial for facebook authentication:

http://coronalabs.com/blog/2013/07/30/understanding-facebook-authentication/

Rob

Hi Rob,

I really appreciate your help and time you spent on writing and explaining this.

BUT I have read everything about generating hash key I found, I did it several times.

I generated the hash with my production keystore, everything went well (asking for password etc.).

I entered that hashkey into facebook site.

As I wrote above, the login WORKS if the facebook app is NOT installed.

If it is installed, I am getting the above error.

Also, what seems to have helped some people, doesn’t help in my case - entering the hash key reported in the error in the facebook dashboard.

I have searched the net and found that you have to replace the _ with / in this error-hash - didn’t help.

So am I really clueless right now.

Any additional help would be really appreciated.

Regards,

Damir.

After hours and hours of trying everything possible I finally found a solution!

First, for the facebook web-login (no native app installed) the hash DOES NOT MATTER, it works even with a wrong one.

Second, the keytool command obviously generated a totally wrong hash for my production keystore.

So I found this peace of code on the net and run it on one of my native games:

try { PackageInfo info = getPackageManager().getPackageInfo("com.package.mypackage", PackageManager.GET\_SIGNATURES); for (Signature signature : info.signatures) { MessageDigest md = MessageDigest.getInstance("SHA"); md.update(signature.toByteArray()); String sign=Base64.encodeToString(md.digest(), Base64.DEFAULT); Log.e("MY KEY HASH:", sign); Toast.makeText(getApplicationContext(),sign, Toast.LENGTH\_LONG).show(); } } catch (NameNotFoundException e) { } catch (NoSuchAlgorithmException e) { } &nbsp;

Guess what ?

Got almost the same hash as the one in the log, except _ is replaced with / and - with +    !!!

Hi, Also Facing same issue. And follow your article to generate same key. But I am not getting where i have to update this new hash key… please guid here.

@shubhampatni86, look at the the device’s console long using “adb logcat”.  If you don’t know how to do that, see:

http://coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/

Then look for the error message:
 

Key hash xxxxxxxxxxxxxxxxxxxxx does not match any stored key hashes

where “xxxxxxxxxxxxxxxxxxxxxx” is the key has that Facebook is expecting, except that you have to replace the _ with a / and the - with a +.  Enter that into Facebook’s developer portal and you should be good to go.

Hi, Also Facing same issue. And follow your article to generate same key. But I am not getting where i have to update this new hash key… please guid here.

@shubhampatni86, look at the the device’s console long using “adb logcat”.  If you don’t know how to do that, see:

http://coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/

Then look for the error message:
 

Key hash xxxxxxxxxxxxxxxxxxxxx does not match any stored key hashes

where “xxxxxxxxxxxxxxxxxxxxxx” is the key has that Facebook is expecting, except that you have to replace the _ with a / and the - with a +.  Enter that into Facebook’s developer portal and you should be good to go.