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


当前回答

将此代码添加到您的活动的onCreate,它将在您的logCat的KeyHash标签下打印散列

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

}
catch (NoSuchAlgorithmException e) {

}

您可以为您的帐户添加多个hashkey,所以如果您一直在调试中运行,不要忘记在发布模式中再次运行此命令。

其他回答

在一个类似的问题上,我发现这对我很有效:

将你想知道散列值的apkname.apk文件复制到“Java\jdk1.7.0_79\bin”文件夹中 执行keytool -list -printcert -jarfile apkname.apk 复制SHA1值并使用此站点进行转换 使用转换后的Keyhash值(例如:zaHqo1xcaPv6CmvlWnJk3SaNRIQ=)

这是Facebook官方页面上给出的内容:

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

让我把这个命令分解成几个片段。

Look for "keytool.exe". You can search that on the C: drive. You can find it in "java jdk" or "java jre". If you have installed multiple versions, choose any. Open a CMD prompt and go to the above directory where you found "keytool.exe". Clip the "exe`" and paste the above command provided on the Facebook page. You will get an error on entering this that OpenSSL is not recognized as in input output command. Solution : Download "Openssl" from OpenSSL (if you have a 64-bit machine you must download openssl-0.9.8e X64). Extract and save it anywhere... I saved it on the C: drive in the OpenSSl folder Replace the openssl in the above command in which you was getting an error of OpenSSL with "C:\OpenSSL\bin\openssl" at both the places after the pipe, "|". If prompted for a password, enter android.

你会得到你的哈希键。要了解进一步的步骤,请再次参考Facebook页面。

Linux

开放式终端:

用于调试构建

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

你会发现debug。Keystore来自”。Android "文件夹复制并粘贴在桌面上,并运行上述命令

发布版本

keytool -exportcert -alias <aliasName> -keystore <keystoreFilePath> | openssl sha1 -binary | openssl base64

注意:确保在这两种情况下,它必须要求密码。如果它不要求输入密码,则意味着命令中有错误。

使用它在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){

    }

我创建了一个适用于Windows和Mac OS x的小工具,只需放入密钥存储文件,并获取散列密钥。

如果您想要默认调试。Keystore文件,使用默认别名和密码。否则,使用您自己的密钥存储库文件和值。

看看吧,下载Windows版或Mac OS X版 (Dev-Host有时可能会宕机…所以如果链接坏了,PM我,我会修复它)。

我希望这对你们有帮助……

2014年12月31日-编辑: 更改主机为AFH。 如果链接坏了,请告诉我

2013年11月21日-编辑:

根据用户的要求,我添加了一个默认的密钥存储库位置和一个捐赠按钮。如果我帮了你,尽管用吧。:)