我在登录时遇到问题。我使用下面的代码登录到Facebook但它给我无效密钥哈希错误。我也向Facebook开发人员添加了哈希密钥,但我不知道无法登录Facebook的问题是什么?以前它是登录,但现在它给出了hashkey错误
public void loginToFacebook() {
mPrefs = getPreferences(MODE_PRIVATE);
access_token = mPrefs.getString("access_token", null);
expires = mPrefs.getLong("access_expires", 0);
if (access_token != null) {
facebook.setAccessToken(access_token);
// btnFbLogin.setVisibility(View.INVISIBLE);
// Making get profile button visible
// Making post to wall visible
// Making show access tokens button visible
Log.d("FB Sessions", "" + facebook.isSessionValid());
}
if (expires != 0) {
facebook.setAccessExpires(expires);
}
if (!facebook.isSessionValid()) {
facebook.authorize(this,
new String[] { "email"},
new DialogListener() {
@Override
public void onCancel() {
// Function to handle cancel event
}
@Override
public void onComplete(Bundle values) {
// Function to handle complete event
// Edit Preferences and update facebook acess_token
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token",
facebook.getAccessToken());
editor.putLong("access_expires",
facebook.getAccessExpires());
editor.commit();
// Making Login button invisible
// btnFbLogin.setVisibility(View.INVISIBLE);
// Making logout Button visible
getProfileInformation();
// Making post to wall visible
// Making show access tokens button visible
}
@Override
public void onError(DialogError error) {
// Function to handle error
}
@Override
public void onFacebookError(FacebookError fberror) {
// Function to handle Facebook errors
}
});
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
答案 0 :(得分:2)
如果它工作之前我会建议等待几个小时,包括我在内的这个问题从今天早上起就面临这个问题。
https://developers.facebook.com/bugs/1755756591335035/?comment_id=2027873617436930
根据官方会员的评论,它应该会在接下来的几个小时内得到解决。
答案 1 :(得分:0)
通过在setContentView()
之后放入Main Activty中的代码生成您的哈希键try {
PackageInfo info = getPackageManager().getPackageInfo(
"yourpackegename", //set your package name
PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.e("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (PackageManager.NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
复制hashCode From Log and past在你的facebook仪表板
答案 2 :(得分:0)
您可以使用两个步骤获取哈希键。一个是通过命令提示符。另一个是通过编码。通过命令提示符仅在第一次处理时使用哈希键。所以尝试使用以编程方式生成的hashkey。
在OnCreate()
中try {
PackageInfo info = getPackageManager().getPackageInfo(
"com.example.packagename(your package name)",
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) {
}
运行代码后,您将在logcat中获取hashkey。在Facebook开发者帐户页面的设置页面中输入该hashkey。