Monday, May 05, 2014

Bad Key Hash for Facebook Apps


When you are creating application for Facebook on Android platform, Developer Website requires the Key Hash for every development device.

This secret stroke is automatically generated by Eclipse or other dev studio in "debug.keystore" storage.

You can obtain Key Hash via command line:

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

or sample code:

...
   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Temporary code to print out the key hash
        try {
            PackageInfo info = getPackageManager().getPackageInfo(
                    "com.Your.Application.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 (NameNotFoundException e) {
        } catch (NoSuchAlgorithmException e) {
        }
...

You have to add all you developer and release key hashes into Web panel of your Facebook Application. Everything looks simply enough.

But, if your hash key contains / (slash) or + (plus) symbol Facebook integration will not work! You'll get error message, something like that: "Key hash Fj2DQ/kUxqHuaTq1jy9iTejN2Uk= does not match any stored key hashes".

I've spent few hours to understand the problem and find a workaround :( Here is an easiest way:

  1. Delete "debug.keystore" file in your "[%USERNAME%]/.android" folder.
  2. Rebuild project to get new "debug.keystore" file with new key hash.
  3. If new hash contains any symbol (= at the end is OK :)) repeat again

Hope this tip helps other developers! Sometimes, development for Android and Facebook is a pain...


No comments: