问题
I have imported the Facebook SDK to my android project, first time I logged in to Facebook from my android application it worked, but the second time I got this error :
Invalid key hash. The key hash "..." does not match any stored key hashes. Configure your app key hashes at http://developers.facebook.com/apps/..
I also tried to enter the key they provided in this error but it didn't work.
EDIT: I have used the cmd command to which it generates a key and asks for the password which is "android"
回答1:
As I understand you've got your key hash, but still I'll put here the code for getting it in the console.
PackageInfo info;
try {
info = getPackageManager().getPackageInfo("com.your.project.package", 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()));
Log.e("hash key", something);
}
} catch (PackageManager.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());
}
Where "com.your.project.package"
is the package of your project =)
Next thing, dont delete previous key hash, they don't conflict I think. For example I have 2 keyhashes in my facebook app.
And the latest thing, and I think this is the problem. Go to the Status&Review of your app at the developers.facebook. And switch your application to public.

回答2:
Keyhashes differs with .apk files generated from different systems. So best solution is - when you get error
"Invalid key hash. The key hash
"<KEYHASH>"
does not match any..."
then write or Copy that "<KEYHASH>"
and Put that in facebook developer page by go to apps-> settings-> android
. If doesn't work then put "="
after keyhash you copied from error and try again. And don't forget to live your project.
回答3:
You should get new key;
Step 1. If you dont have OpenSSL lib, Download here
Step 2. And Extract it. create a folder- OpenSSL in C:/ and copy the extracted code here.
Step 3. And open cmd and go C:\Program Files\Java\jdk1.7.0_79\bin
Step 4. And execute this command -
keytool -exportcert -alias androiddebugkey -keystore %HOMEPATH%\.android\debug.keystore | "C:\OpenSSL\bin\openssl" sha1 -binary |"C:\OpenSSL\bin\openssl" base64
Step 5. Get key and go developer facebook website
Step6. Add key to your project.
That's it.
回答4:
Add the key Provided in your app on
http://developers.facebook.com/apps/
Additionally, in last add "="
symbol
ex. you are getting 2jmj7l5rSw0yVb/vlWAYkK/Y
than in Facebook page,
add 2jmj7l5rSw0yVb/vlWAYkK/Y=
That Last "="
sign is important.
I spent an hour generating the keyhashes, than tried this trick.
回答5:
there are two possible solutions that might help, each one of these got it done for me in different occassions so here we go.
(assumming that you typed the hash key that the app told you to and didn't work you)
- You should delete de debug.keystore file located usually in "C:\Users\yourUserName.android", then recompile the project, and get the new hash key with thes new debug.keyhash (as stated here ). Then these new keyhash should work.
2.These is a little more tricky. Maybe the reason why your facebook developer dashboard key hash doesn't work even if you typed it, is because your facebook app ID has changed by the moment you built the apk (this happened to me because i had previous versions of my app and it was giving me the past id, even though i had it specified in config.xml). So, in my case i have a cordova project so i went to "C:your_CordovaProject\platforms\android\res\values\facebookconnect.xml" and open the file, here you gotta check if the fb_app_id is the same that you have in your facebook dashboard. if it's not (it wasn't in my case) what you have to do is --Remove your facebook plugin, --add it again and --specify your app id and name one more time like this:
cordova plugin add cordova-plugin-facebook4 --save --variable APP_ID="123456789" --variable APP_NAME="myApplication"
then i did the hash key process again and it worked. Hope it helps
来源:https://stackoverflow.com/questions/30934225/facebook-android-app-error-invalid-key-hash