我正在开发一个Android应用程序,我想在其中集成一个Facebook 发布功能。我下载了Facebook-Android SDK,然后 readme。Md(文本文件)在那里,其中提到要生成 Android的键散列。我如何生成它?
当前回答
facebook开发者网站上的官方文件:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Add code to print out the key hash
try {
PackageInfo info = getPackageManager().getPackageInfo(
"com.facebook.samples.hellofacebook",
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) {
}
其他回答
这是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页面。
Linux
开放式终端:
用于调试构建
keytool -exportcert -alias androiddebugkey -keystore debug.keystore | openssl sha1 -binary | openssl base64
你会发现debug。Keystore来自”。Android "文件夹复制并粘贴在桌面上,并运行上述命令
发布版本
keytool -exportcert -alias <aliasName> -keystore <keystoreFilePath> | openssl sha1 -binary | openssl base64
注意:确保在这两种情况下,它必须要求密码。如果它不要求输入密码,则意味着命令中有错误。
我找到的最简单的解决方法是:
打开原木猫 尝试使用Android SDK访问Facebook 在日志中寻找这样的行: 04-24 01:14:08.605: I/System.out(31395): invalid_key:Android key mismatch。 您的密钥“abcdefgHIJKLMN+OPqrstuvwzyz”与您的 应用程序设置。检查您的应用程序设置 http://www.facebook.com/developers 复制“abcdefgHIJKLMN+OPqrstuvwzyz”,并将其粘贴到Facebook Android密钥哈希区。
在一个类似的问题上,我发现这对我很有效:
将你想知道散列值的apkname.apk文件复制到“Java\jdk1.7.0_79\bin”文件夹中 执行keytool -list -printcert -jarfile apkname.apk 复制SHA1值并使用此站点进行转换 使用转换后的Keyhash值(例如:zaHqo1xcaPv6CmvlWnJk3SaNRIQ=)
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());
}
}
});
}
}
推荐文章
- 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>"没有与主机名关联的地址