Android-Facebook 应用程序的密钥哈希

Posted

技术标签:

【中文标题】Android-Facebook 应用程序的密钥哈希【英文标题】:Key hash for Android-Facebook app 【发布时间】:2011-05-22 07:11:07 【问题描述】:

我正在开发一个 android 应用,我想在其中集成一个 Facebook 发布功能。我下载了 Facebook-Android SDK,我得到了 那里的readme.md(文本文件),其中提到生成 Android 的密钥哈希。如何生成它?

【问题讨论】:

您可以查看此链接javatechig.com/2012/12/10/… 检查this对于那些仍然面临问题的人, 使用这个为调试和发布模式生成HashKey。 ***.com/questions/7506392/… 获取钥匙观看this视频 这里是完整的解决方案***.com/a/68718505/8663316 【参考方案1】:

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());
    

请不要忘记在调试和发布环境中生成密钥,因为它们会根据构建设置而变化。

【讨论】:

【参考方案2】:

试试这个:

获取哈希键值的两种方式

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

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

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());
            
        

【讨论】:

【参考方案3】:

这里是 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)
    

    

【讨论】:

【参考方案4】:
This required no coding input. Go to Android Studio-> 
Click on Right side panel "Gradle"=>
*Your App Name =>
*Your App Name(root)=>
*Tasks=>
*android =>
*Double click on **signinReport**=>
Will get 
example :
SHA1: [![6A:DE:ED:5A:9F:0B:19:47:38:DC:DE:3B:7B:A2:D7:4C:6C:0A:24:70][1]][1]

Go to 

http://fbkeyhash.com/index.php
Paste your SHA-1

【讨论】:

【参考方案5】:

您可以从 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")
    

【讨论】:

以上是关于Android-Facebook 应用程序的密钥哈希的主要内容,如果未能解决你的问题,请参考以下文章

在android中使用xmpp的android-facebook聊天客户端

Android - Facebook 集成:无法导入 com.facebook.Session

当我们知道HashMap中的值时获取密钥[重复]

Excel2019产品密钥是啥?求个能用的Excel2019产品密钥

打开软件时显示“发现自动许可证管理器存在问题”。。?怎样将西门子PLC的密钥 --STEP.7.V5.4.密钥(无限期

微信小程序框架分析小练手——仿香哈菜谱小程序制作