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


当前回答

我只是为这个确切的目的做了一个工具,即https://keyhash.vaibhavpandey.com/。它比其他任何方法都简单,因为它需要您浏览计算机上的密钥存储库,并输入密码,分别为谷歌和Facebook生成SHA-1十六进制和Base64版本。

不必担心密钥库或密码短语,因为工作完全是在浏览器中完成的,您可以检查网络选项卡,该工具也是开源的https://github.com/vaibhavpandeyvpz/keyhash。

其他回答

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

最简单的解决方案:

不加哈希键,实现其他所有东西 当facebook登录被按下,你会得到一个错误说“无效的键哈希。密钥哈希“xxx”不匹配任何存储密钥. ...” 打开facebook应用程序仪表板,并添加哈希值“xxx=”(“xxx”哈希值来自错误+“=”符号)

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.os.Bundle;
import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.Signature;
import android.text.Editable;
import android.util.Base64;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

    Button btn;
    EditText et;
    PackageInfo info;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn=(Button)findViewById(R.id.button1);
        et=(EditText)findViewById(R.id.editText1);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                try {
                    info = getPackageManager().getPackageInfo("com.example.id", PackageManager.GET_SIGNATURES);
                    for (Signature signature : info.signatures) {
                        MessageDigest md;
                        md = MessageDigest.getInstance("SHA");
                        md.update(signature.toByteArray());
                        String something = new String(Base64.encode(md.digest(), 0));
                        //String something = new String(Base64.encodeBytes(md.digest()));
                        et.setText("" + something);
                        Log.e("hash key", something);
                    }
                } catch (NameNotFoundException e1) {
                    Log.e("name not found", e1.toString());
                } catch (NoSuchAlgorithmException e) {
                    Log.e("no such an algorithm", e.toString());
                } catch (Exception e) {
                    Log.e("exception", e.toString());
                }
            }
        });
    }



}

在Android Studio中也解决了我的问题,但方法略有不同。

在Android Studio中获取SHA-1值。

点击它 点击签署报告 sha - 1副本

SHA-1值是这样的 CD: A1: EA: A3:5C: 5 c: 68: FB:费尔南多-阿隆索:0:6 b: E5:5A: 72:64: DD: 26:8D: 44:84 打开http://tomeko.net/online_tools/hex_to_base64.php进行转换 你的SHA1值到base64。这就是Facebook所要求的 生成的哈希 " ********************= " 并复制关键散列 facebook应用程序控制台。

部分答案来自Github链接

这是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页面。