我正在开发一个Android应用程序,我想在其中集成一个Facebook 发布功能。我下载了Facebook-Android SDK,然后 readme。Md(文本文件)在那里,其中提到要生成 Android的键散列。我如何生成它?
当前回答
这是Xamarin的版本
private void printKeyHash()
{
try
{
PackageInfo info = PackageManager.GetPackageInfo(PackageName, PackageInfoFlags.Signatures);
foreach (var signature in info.Signatures)
{
MessageDigest md = MessageDigest.GetInstance("SHA1");
md.Update(signature.ToByteArray());
var hash = Base64.EncodeToString(md.Digest(), Base64Flags.Default);
Log.Debug("KeyHash:", hash);
}
}
catch (PackageManager.NameNotFoundException e)
{
}
catch (NoSuchAlgorithmException e)
{
}
}
其他回答
将此代码添加到您的活动的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,所以如果您一直在调试中运行,不要忘记在发布模式中再次运行此命令。
您可以从SHA-1密钥获得密钥哈希。 这很简单,你需要得到你的SHA-1(签名APK)密钥从Play Store检查下图。
现在复制SHA-1密钥并在这个网站http://tomeko.net中过去,也检查下面的图像以获得您的密钥哈希。
Kotlin代码获取哈希键
private fun logHashKey() {
try {
val info = getPackageManager().getPackageInfo("your.package.name", PackageManager.GET_SIGNING_CERTIFICATES);
for (signature in info.signingInfo.signingCertificateHistory) {
val md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
val something = Base64.getEncoder().encodeToString(md.digest());
Log.e("hash key", something);
}
} catch (e1: PackageManager.NameNotFoundException) {
Log.e("name not found", e1.toString());
} catch (e: NoSuchAlgorithmException) {
Log.e("no such an algorithm", e.toString());
} catch (e: Exception) {
Log.e("exception", e.toString());
}
}
请不要忘记在调试和发布环境中生成键,因为它们会随着构建设置而变化。
这是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页面。
试试这个:
两种方法来获得哈希键值
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());
}
}
推荐文章
- apk (.apk)和应用程序包(.aab)的区别
- React-Native:应用程序未注册错误
- 如何设置超时在改造库?
- 无法在Windows上从/usr/local/ssl/openssl.cnf加载配置信息
- Android - SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度
- TextView的字体大小在Android应用程序改变字体大小从本机设置
- 如何模拟Android杀死我的进程
- 禁用EditText闪烁光标
- Android Eclipse -无法找到*.apk
- 设置TextView文本从html格式的字符串资源在XML
- 如何允许所有网络连接类型HTTP和HTTPS在Android(9)馅饼?
- Android加载JS包失败
- Android Studio, logcat在应用程序关闭后清理
- 在android中从上下文获取活动
- 无法解析主机"<URL here>"没有与主机名关联的地址