0

I use this code to generate my app keyhash:

public static String getKeyHash(Context context) {
    String returner = "";
    try {
        PackageInfo info = context.getPackageManager().getPackageInfo(
                "com.abc.mypackage", PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            returner = Base64.encodeToString(md.digest(), Base64.DEFAULT);
            Log.e(TAG, Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        Log.e(TAG, e.toString());
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        Log.e(TAG, e.toString());

    }
    return returner;
}

The function is called from onCreate function of Main Activity. And the generated hash is

ki4PhEa/cy/qsP/omrTEPkYFCDE=

Due to this Android app Key Hash doesn't match any stored key hashes, I replaced all the / to _ and finally I get this

ki4PhEa_cy_qsP_omrTEPkYFCDE=

I put the keyhash to the Facebook developer site and save

enter image description here

When I login to Facebook, my app gives this exception (awesome !!!!)

02-26 23:05:49.008    4966-4966/? E/MyClassListViewAdapter﹕ Bad thing happened com.facebook.FacebookAuthorizationException: UnknownError: ApiException:Key hash ki4PhEa_cy_qsP_omrTEPkYFCDE does not match any stored key hashes.

How can I solve this ridiculous problem ?

Update This problem only occurs on my real device (Htc One X). It does not occur when running on emulator

Community
  • 1
  • 1
Thai Tran
  • 9,815
  • 7
  • 43
  • 64

2 Answers2

0

You need to use keytool to generate your key hash from the certificates that are used when you sign your app.

keytool -exportcert -alias <RELEASE_KEY_ALIAS> -keystore <RELEASE_KEY_PATH> 
| openssl sha1 -binary | openssl base64

You can do it for both your debug and release keystores.

Robby Pond
  • 73,164
  • 16
  • 126
  • 119
  • I used `keytool` at the first time, and got that exception so I switched to use the hash calculating function. By the way, I just run the keytool again and it gives the result `bLxqLn/RXHcNbPbGMPFfbQPDlY8=`. I replaced `/` to `_` again and put to facebook developer site. Again, still does not work on my real device. Please have a look at my updated question – Thai Tran Feb 26 '14 at 16:10
0

I seriously have no idea but it started to work after I add the key with the / (instead of removing it)

ki4PhEa/cy/qsP/omrTEPkYFCDE=

enter image description here

Thai Tran
  • 9,815
  • 7
  • 43
  • 64