6
votes

In the Facebook android tutorial we are told to use following code to create a key hash:

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

Is this the exact code to use in all situations? For example instead of ~/.android/debug.keystore should it something like C:/folderone/foldertwo/.android/debug.keystore?

As you can see I'm unsure of whether inverted commas are required or not, whether full paths are required or not!

Is anyone able to provide a real world example?

See
https://developers.facebook.com/docs/mobile/android/build/#sso

9
you need have openssl from google... follow the steps given in the [link][1] [1]: stackoverflow.com/questions/4388992/…Nixit Patel
You can see real example here stackoverflow.com/questions/9977492/…dimetil
You should find the answer by Avi here.abosamy

9 Answers

16
votes

try

try {
PackageInfo info = getPackageManager().getPackageInfo("com.eatapp", PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
    MessageDigest md = MessageDigest.getInstance("SHA");
    md.update(signature.toByteArray());
    Log.e("MY KEY HASH:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (NameNotFoundException e) {

} catch (NoSuchAlgorithmException e) {

}

in your main Activity :-) This is the only solution it works for me for Android SDK 3.0

11
votes

You can create this way

keytool -exportcert -alias androiddebugkey -keystore c:\Users\<your windows default user>\.android\debug.keystore | openssl sha1 -binary | openssl base64

Enter keystore password: android

3
votes
 /**
     * Generates the hash key used for Facebook console to register app. It can also be used for other sdks) Method copied from: https://developers.facebook.com/docs/android/getting-started/
     */
    public static String printHashKey(Context ctx) {
        // Add code to print out the key hash
        try {
            PackageInfo info = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), PackageManager.GET_SIGNATURES);
            for (Signature signature : info.signatures) {
                MessageDigest md = MessageDigest.getInstance("SHA");
                md.update(signature.toByteArray());
                return Base64.encodeToString(md.digest(), Base64.DEFAULT);
            }
        } catch (NameNotFoundException e) {
            return "SHA-1 generation: the key count not be generated: NameNotFoundException thrown";
        } catch (NoSuchAlgorithmException e) {
            return "SHA-1 generation: the key count not be generated: NoSuchAlgorithmException thrown";
        }

        return "SHA-1 generation: epic failed";
    }
2
votes

In eclipse, window -> preferences -> Android -> build -> default debug keystore, copy the path to replace the ~/.android/debug.keystore

2
votes

When having the error in the log, when trying to login to Facebook, look for something that looks like:

Invalid key hash. The key hash *** does not match any stored key hashes. Configure your app key hashes at http://developers.facebook.com/apps/565561836797777

where "***" is the key that you need to use.

1
votes

I had the same problem, I spend a couple of hours to find a solution, but actually the Facebook SDK provides the solution by itself.

in the DialogListener class I modified the onFacebookError method:

@Override 
public void onFacebookError(FacebookError error) {
   Log.d("myTag",error.getmessage); 
 }

Execute the app (which was sign with the same key i use for the market), and on LogCat will be a message under this tag with the correct key.

We had also created a simple project which does all the work, and returns the correct key on an alert-box and on LogCat. You can find it on our blog.

1
votes

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

if you haven't setup environment variables for open ssl and java sdk than put jdk's bin folder path in place of keytool and your openssl path in place of openssl and not to forget to put double quotes for your path

ex-"C:\Program Files\Java\jdk1.5.0_11\bin" -exportcert -alias androiddebugkey -keystore "C:\Users\amin.android\debug.keystore" | "F:\openssl\binsha1\openssl.exe" -binary | "F:\openssl\binsha1\openssl.exe" base64

1
votes

One brute force option is to just go ahead and try to share something from your app. My app then displays a Facebook page with the key it is trying to match. Then you can just copy this key and put it in your Facebook 'Settings' page on your developer Facebook account.

Not ideal, but in a pinch it may be helpful.

0
votes

There are two ways to generate Hashkey for Facebook.

  1. You can use the following code snippet

     try {
         PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_SIGNATURES);
         for (Signature signature : info.signatures) {
             MessageDigest messageDigest= MessageDigest.getInstance("SHA");
             messageDigest.update(signature.toByteArray());
             String hashKey = new String(Base64.encode(messageDigest.digest(), 0));
             Log.i("Hash Key ", "value is " + hashKey);
         }
     } catch (NoSuchAlgorithmException e) {
         Log.e("Exception ", "is ", e);
     } catch (Exception e) {
         Log.e("Exception ", "is ", e);
     }
    
  2. You can create Hashkey for Facebook Online by pasting your SHA1 on This link

Happy Coding :)