我正在开发一个Android应用程序,我想在其中集成一个Facebook 发布功能。我下载了Facebook-Android SDK,然后 readme。Md(文本文件)在那里,其中提到要生成 Android的键散列。我如何生成它?
当前回答
下载openSSL for windows,你可以在这里找到64位和32位的 解压缩下载的文件 在C盘中创建文件夹名为openSSL 将所有提取的项目复制到openSSL文件夹(bin,include,lib, openSSL .cnf) 获取android调试密钥库,默认位置为
C:\Users\username\.android\ debug.keystore
现在获得命令提示符并粘贴这段代码
keytool -exportcert -alias androiddebugkey -keystore C:\Users\username.android\debug.keystore |“C:\openSSL\bin\openssl” sha1 -binary |“C:\openSSL\bin\openssl” base64
按enter键,你会得到28位的键码
其他回答
对于Android应用程序
这段代码用于在Android应用程序中获取Facebook集成的哈希键。我已经测试了所有的设备,一切正常。只更改此代码的包名:
private void facebookHashKey() {
try {
PackageInfo info = getPackageManager().getPackageInfo("com.app.helpcove", PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
String hashCode = Base64.encodeToString(md.digest(), Base64.DEFAULT);
System.out.println("Print the hashKey for Facebook :"+hashCode);
Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
}
以下是步骤
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
欲了解更多信息,请访问这里
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());
}
}
});
}
}
这是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页面。
这个方法对我来说很有效,我做了一些观察:
每个SHA1密钥都有一个对应的密钥散列,长度为28个字符,以'='结尾 我尝试过在线工具来获取我的SHA1的哈希键,但这个哈希键从来没有为我工作过。 如果你在windows上安装Open -ssl:Open ssl 我已经在我拥有的所有密钥存储库文件上使用了这个keytool命令,即登台、调试和发布。
keytool -exportcert -alias my_alias_name -keystore "C:\Users\...my_filename.jks" | "C:\Openssl\bin\openssl.exe" sha1 -binary | "C:\Openssl\bin\openssl.exe" base64
注意: 上面的方法总是会给你一个键散列,即使你填错了任何参数。知道你是否得到正确的散列键的技巧是——运行这个命令后,如果提示输入密码,这意味着后续的键是正确的。
不要使用my_filename。密钥存储库,但使用my_filename代替。jks。
推荐文章
- Facebook:永久页面访问令牌?
- 禁用在android中打开导航抽屉的滑动手势
- Android:允许平板电脑竖屏和横屏,但强制手机竖屏?
- 如何使布局与视图填充剩余的空间?
- 字符串资源新行/n不可能?
- 如何点击或点击一个TextView文本
- Android房间-简单的选择查询-不能访问数据库在主线程
- Android: ScrollView vs NestedScrollView
- Git克隆/拉不断冻结在“存储密钥在缓存?”
- 错误:无法运行mksdcard SDK工具
- 未附加到窗口管理器的视图崩溃
- 将焦点设置为EditText
- 如何把空格字符放入XML字符串名?
- 我可以设置“android:layout_below”在运行时编程?
- Android Studio谷歌JAR文件导致GC开销限制超过错误