How to create a Facebook key hash?

前端 未结 9 1207
独厮守ぢ
独厮守ぢ 2020-12-14 13:15

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

keytool -exportcert -alias androiddebugkey -keystore ~/.andro

9条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-14 14:04

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

提交回复
热议问题