Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Facebook key hash?

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

like image 808
Mel Avatar asked Apr 06 '12 10:04

Mel


People also ask

How do I get a key hash on Facebook?

Go to the Facebook Developer site. Log into Facebook and, using the dropdown menu in the top-right, go to Developer Settings: In your developer settings, select Sample App from the menu, and add and save your key hash into your profile: You can add multiple key hashes if you develop with multiple machines.

How do I convert SHA1 to key hash on Facebook?

If you don't have any idea how to get the SHA1 Fingerprint then you can go through the example How you can get the SHA1 Fingerprint for Android. After getting the SHA1 you have to open a link tomeko.net where you will see the option to insert SHA1 and it will convert your SHA1 into Key Hash.


3 Answers

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

like image 137
cesards Avatar answered Nov 11 '22 01:11

cesards


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

like image 41
kodeshpa Avatar answered Nov 11 '22 02:11

kodeshpa


 /**
     * 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";
    }
like image 27
nightfixed Avatar answered Nov 11 '22 00:11

nightfixed