How to create a Facebook key hash?

2020-02-08 09:53发布

问题:

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

回答1:

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



回答2:

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:

 /**
     * 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";
    }


回答4:

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



回答5:

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.



回答6:

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.



回答7:

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



回答8:

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.