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


当前回答

试试这个:

两种方法来获得哈希键值

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

其他回答

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



}

以下是步骤

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

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

你需要通过keytool为android签名应用程序创建一个密钥库,就像android站点中描述的过程一样,然后你必须安装cygwin,然后你需要从谷歌代码安装openssl,然后只需执行以下命令,你就会得到android的哈希密钥,然后把哈希密钥放入你创建的facebook应用程序。然后你可以通过Android应用程序访问facebook应用程序的张贴墙(“publish_stream”)可以是一个例子。

$ keytool -exportcert -alias alias_name -keystore sample_keystore。Keystore | openssl sha1 -binary | openssl base64 . Keystore | openssl sha1 -binary

您需要从cygwin执行上面的命令。

keytool -exportcert -alias androiddebugkey -keystore       C:\Users\pravin\.android\debug.keystore | "H:\OpenSSL\bin\openssl" sha1 -binary | "H:\OpenSSL\bin\openssl" base64

这对我很管用……

步骤:

1) Open command line go to - > java Keytool..... for me C:\Program Files\Java\JDK1.7\bin
2) Download OpenSSL from google
3) paste this with changing your paths -
   keytool -exportcert -alias androiddebugkey -keystore C:\Users\pravin\.android\debug.keystore | "H:\OpenSSL\bin\openssl" sha1 -binary | "H:\OpenSSL\bin\openssl" base64 

    ....................   give proper debug.keystore path and openSSL path .. 

4) Finley it may be ask u password .. so give password -> android   ...
5) you will get 28 characters that will be your has key

有两种方法可供选择,一种是复杂的,一种是简单的

方法一:(小复杂)

首先,你必须下载相应的64位或32位ssl,记住下载版本号openssl-0.9.8e_X64.zip或openssl-0.9.8e_WIN32.zip后面有e的文件,而不是版本号后面有k的文件,

并放置在AndroidStudio/jre/bin目录下,如果你不知道放在哪里,你可以通过右键单击android studio快捷方式找到这个目录:

现在您已经在一个地方管理了两个必需的东西,但是仍然需要找到调试的路径。在“C:\Users\yourusernamehere\.android\debug.keystore”中可以找到。

如果您的应用程序已经发布或即将发布,那么当且仅当您正在测试时,请使用您的发布签名密钥库 在开发模式下可以使用调试、按键

一切都设置好后,让我们以base64格式安排您想要执行的用于生成哈希键的命令,您的命令将如下所示

keytool.exe -exportcert -alias androiddebugkey -keystore "C:\Users\ayyaz talat\.android\debug.keystore" | "D:\Program Files\Android\Android Studio\jre\bin\openssl\bin\openssl.exe" sha1 -binary |"D:\Program Files\Android\Android Studio\jre\bin\openssl\bin\openssl.exe" base64

它将提示您为调试输入密码。密钥存储库,默认为android。如果您使用自己的密钥,那么密码也将是您的。 如果一切顺利,输出将如下所示,希望对您有所帮助

第二种方法(分别为简单方法)

如果你不想经历以上所有的过程,那么只需使用下面的方法来记录haskey:

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

输出: