i just implemented Facebook log in into my android application. when i try to log in with Facebook i got following exception in log-cat.
Error Log :
com.facebook.http.protocol.ApiException: Key hash oZgj_um2MGi1eYpfTqwytjLMN10 does not match any stored key hashes
I already added this key hash into my developer account app page.But still i am facing same issue.
Your HashKey is wrong. It should have 28 characters while your hash key 27 characters the hashkey always ends with =. So I think you have missed it. Please check it again and
Change your hashkey by generating it programatically from the following code given in Facebook Docs and defined at Facebook Integration in Android Application
PackageInfo info;
try {
info = getPackageManager().getPackageInfo("com.example.yourpackagename", 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 (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());
}
Related
I've created an app which uses the facebook login. I've added the key hash to the facebook developer page and it worked fine in all devices.
Now I've uploaded the app to google play, and when I try to login, it sais that the key hash does not match any stored key hashes.
I copied the key hash in the error message and pasted it. Still - doesn't work.
I tried to generate a key hash in cmd using the release key store - no luck.
Does anyone know what is the problem and how to fix it?
Thanks in advance!
Try adding the following code snippet to your app while it's signed with the release certificate, compare the hash output with the one you submitted to the Facebook portal:
PackageInfo info;
try {
info = getPackageManager().getPackageInfo("com.package.name", PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md;
md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
String hash= new String(Base64.encode(md.digest(), 0));
Log.e("hash", hash);
}
} 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());
}
I got hash key from below code in android:
try {
PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(),
PackageManager.GET_SIGNATURES);
for (Signature signature : packageInfo.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
}
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());
}
But it was invalid key... Please suggestion how to verify hash key for Facebook login....
You are getting the hash-key with debug key... Which may work if you haven't sign the package and running app in debug mode. What you need to do is :
1) Go to the manifest file and add to the application android:debuggable="true".
2) Sign the application, capy and install to your device manually or for use "adb install path_to_apk" from command line.
3) Now run your app and monitor the logcat.
4) You will get printed a new key which will be the matching key with key facebook app is showing in the error msg, The key you have got is now having a = sign in the last.
5) Register this key on facebook developer site
Alternate Trick
You can do one other thing Simply register key which is showing in the error msg "The key hash ### does not match any stored key hashes" Just add the = in the end of the ###. It will be like ###=
you are done!! Hope this will work.
Add this function into your class,and call this function in oncreate methode,then generate sign apk and run sign apk in your device and check log-cat, copy generated hash key to facebook developer console.
private void showHashKey()
{
// Add code to print out the key hash
try {
PackageInfo info = getPackageManager().getPackageInfo(
"com.kisan.kisan",
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 (PackageManager.NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
}
Check facebook authentication,it will work fine
I think the issue is you might not have updated the keyhash in the facebook developer account. Refer the: https://developers.facebook.com/docs/android/getting-started
Generate the Keyhash using:
keytool -exportcert -alias androiddebugkey -keystore %HOMEPATH%\.android\debug.keystore | openssl sha1 -binary | openssl base64
And update into your facebook devloper site, under Apps tab, General Settings
Assign this to a button's on click and get the Key Hash from the logcat.
Button getKeyHash = (Button) findViewById(R.id.button_key_hash);
getKeyHash.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Key Hash
try {
PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(),
PackageManager.GET_SIGNATURES);
for (Signature signature : packageInfo.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} 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());
}
}
});
Now go to your app at https://developers.facebook.com/apps
. Then click on settings, add your Key Hash and save.
I'm trying to generate Key hash to incorporate Facebook Application in my Apps.
The problem is that;
I'm actually getting the right key hash, but i'm still getting the wrong app key hash.
(Maybe I'm not taking the right part from the whole key..)
Any help will be very appreciated, I'm pretty lost here...
Problem Fixed! :
Just needed to get rid of the "=" sign after copying the key from the CMD.
Please let us know how you are getting your Key-Hash. If from keytool, then let us know the procedure, may be we will be able to fix, what are your missing.
In the meantime you can use the following code to get the Key-Hash.
private void getAppKeyHash() {
try {
PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), 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));
Log.d("Hash key", something);
}
}
catch (NameNotFoundException e1) {
// TODO Auto-generated catch block
Log.e("name not found", e1.toString());
}
catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
Log.e("no such an algorithm", e.toString());
}
catch (Exception e){
Log.e("exception", e.toString());
}
}
Now from the logcat you can find the Key-Hash. For more info Please check it form facebook-sdk
Problem Fixed! :
Just needed to get rid of the "=" sign after copying the key from the CMD.
I have recently started working on facebook API, where I came under the situation of generating Hash key and registering it on facebook for further use.
For that, I used the following code
PackageInfo info;
try {
info = getPackageManager().getPackageInfo("com.you.name", 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 (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());
}
All worked well, as I was able to work with facebook in my app.
But, after publishing the app on playstore, I found the error Invalid_Android_key parameter. The key does not match any allowed key Configure your app key hashes at https://developers.facebook.com/apps/..........
Please, let me know the cause of this problem and how to handle this.
I had same Problem, after creating the apk, the key hash is changed! because using this code u get the debug keystore hash, but when creating apk, it's another hash, gotta capture it from log after trying ur apk on emulator , then delete code and export again without this log , i know it's a hassle but for me it was easier than keytool...
I had integrated facebook in my android application .
Generate key using debugkeytool and it works fine on both emulator and real device.
Now i have to make release apk file and i had created keystore using eclipse android tool
to export signed application package .
And using this keystore i had generated new key hash for facebook and set it on facebook developers site.
but still i am not able to post on facebook wall after signing my app with my own created keystore.
I had check all the steps for creating keystore and it is correct.
please help me out of this situation.
Thanks
I got the same error but when i checked the hash key by PackageManager i got the different hash key of the application and update it on facebook and it worked for me.
PackageInfo info;
try {
info = getPackageManager().getPackageInfo("com.example.yourpackagename", 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 (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());
}
change your package name in the code. The hash key will be printed in the log.
It may help you.