我正在开发一个Android应用程序,我想在其中集成一个Facebook 发布功能。我下载了Facebook-Android SDK,然后 readme。Md(文本文件)在那里,其中提到要生成 Android的键散列。我如何生成它?


当前回答

我犯了一个小错误,应该记住。如果你正在使用你的密钥库,那么给出你的别名,而不是androiddebugkey…

我的问题解决了。现在,如果我的设备上安装了Facebook,那么我的应用程序仍然在Facebook登录集成上获得数据。只关心你的哈希键。

请看下文。

C:\Program Files\Java\jdk1.6.0_45\bin>keytool -exportcert -alias here your alias name  -keystore "G:\yourkeystorename.keystore" |"G:\ssl\bin\openssl" sha1 -binary | "G:\ssl\bin\openssl" base64

然后按Enter -它会要求您输入密码,然后输入您的密钥库密码,而不是Android。

酷。

其他回答

使用它在kotlin中打印键哈希

try {
        val info = context.getPackageManager().getPackageInfo(context.packageName,
                PackageManager.GET_SIGNATURES);
        for (signature in info.signatures) {
            val md = MessageDigest.getInstance("SHA")
            md.update(signature.toByteArray())
            Log.d("Key hash ", android.util.Base64.encodeToString(md.digest(), android.util.Base64.DEFAULT))
        }
    }catch (e:Exception){

    }

你可以从Java/Kotlin Activity中打印哈希键。部分代码已弃用,但这里是包含新旧代码的完整解决方案。

private fun printHashKey(context: Context) {
    try {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            val packageInfo: PackageInfo = context.packageManager.getPackageInfo(
                context.packageName,
                PackageManager.GET_SIGNING_CERTIFICATES
            )

            for (signature in packageInfo.signingInfo.apkContentsSigners) {
                val md = MessageDigest.getInstance("SHA")
                md.update(signature.toByteArray())
                Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT))
            }
        } else {
            val packageInfo: PackageInfo = context.packageManager.getPackageInfo(
                context.packageName,
                PackageManager.GET_SIGNATURES
            )

            for (signature in packageInfo.signatures) {
                val md = MessageDigest.getInstance("SHA")
                md.update(signature.toByteArray())
                Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT))
            }
        }

    } catch (e: PackageManager.NameNotFoundException) {
        Log.d(TAG, "printHashKey: PackageManager.NameNotFoundException -> $e")
    } catch (e: NoSuchAlgorithmException) {
        Log.d(TAG, "printHashKey: NoSuchAlgorithmException -> $e")
    }
}

试试这个:

两种方法来获得哈希键值

1)从命令行获取哈希键(官方文档:https://developers.facebook.com/docs/android/getting-started)

keytool -exportcert -alias androiddebugkey -keystore %HOMEPATH%\.android\debug.keystore | openssl sha1 -binary | openssl
base64

OR

2)获取哈希键使用代码

  @Override
   protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);

            //Hask Kay generation 
             GetKeyHase();
    }

    private void GetKeyHase() {
            try {
                PackageInfo info = getPackageManager().getPackageInfo("ADD YOUR PACKAGE NAME", PackageManager.GET_SIGNATURES);
                for (Signature signature : info.signatures) {
                    MessageDigest md = (MessageDigest.getInstance("SHA"));
                    md.update(signature.toByteArray());
                    String hashkey_value = new String(Base64.encode(md.digest(), 0));
                    Log.e("hash key", hashkey_value);
                    //check you logcat hash key value
                }
            }catch (Exception e) {
                Log.e("exception", e.toString());
            }
        }

我找到的最简单的解决方法是:

打开原木猫 尝试使用Android SDK访问Facebook 在日志中寻找这样的行: 04-24 01:14:08.605: I/System.out(31395): invalid_key:Android key mismatch。 您的密钥“abcdefgHIJKLMN+OPqrstuvwzyz”与您的 应用程序设置。检查您的应用程序设置 http://www.facebook.com/developers 复制“abcdefgHIJKLMN+OPqrstuvwzyz”,并将其粘贴到Facebook Android密钥哈希区。

以下是步骤

Download openssl from Google code (If you have a 64 bit machine you must download openssl-0.9.8e X64 not the latest version) Extract it. create a folder- OpenSSL in C:/ and copy the extracted code here. detect debug.keystore file path. If u didn't find, then do a search in C:/ and use the Path in the command in next step. detect your keytool.exe path and go to that dir/ in command prompt and run this command in 1 line- $ keytool -exportcert -alias androiddebugkey -keystore "C:\Documents and Settings\Administrator.android\debug.keystore" | "C:\OpenSSL\bin\openssl" sha1 -binary |"C:\OpenSSL\bin\openssl" base64 it will ask for password, put android that's all. u will get a key-hash

欲了解更多信息,请访问这里