Invalid Hash key in facebook app in android - android

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.

Related

Facebook Invalid key hash on public app

I am getting the "Invalid key hash" error on my Android even though my app is in production and the facebook app is set as public. If I put the key hash into the facebook settings it works fine, but I suppose this would only work on my own device. What am I getting wrong here?
You have to create a release apk and print keyhash using this method. and set that keyHash in fb consol.
public static void printHashKey(Context context)
{
// Add code to print out the key hash
try {
PackageInfo info = context.getPackageManager().getPackageInfo(
com.example.app.BuildConfig.APPLICATION_ID,
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) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
Follow this steps for solution.
Paste this code in your activity.
public static void printHashKey(Context context)
{
// Add code to print out the key hash
try
{
PackageInfo info = context.getPackageManager().getPackageInfo( com.example.app.BuildConfig.APPLICATION_ID, 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)
{
e.printStackTrace();
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
}
Sign your APK.(release APK)
Install generated signed APK to your mobile
Connect your phone with pc.
Now open screen where you put above code.
Here your can see new HashKey in logcat
Paste this HashKey in Facebook developer site where you app created.
Enjoy with your application.

invalid key hash - android facebook

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());
}

how to get hash key for linkedIn account in android using eclipse?

I have tried hash key using .android debug key but still not getting my LinkedIn app connected with my App.
And also getting error like when i link with my Facebook App, LinkedIn also showing linked automatically.
Am i attach Facebook hash key in LinkedIn Account?
Please help me to get the correct hash key for eclipse specially.
public void generateHashkey(){
try {
PackageInfo info = getPackageManager().getPackageInfo(
PACKAGE,
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) {
Log.d(TAG, e.getMessage(), e);
} catch (NoSuchAlgorithmException e) {
Log.d(TAG, e.getMessage(), e);
}
}

Invalid key hash

Hi I'm very new to facebook login. I tried to generate a hash key for facebook android app. The first time it didn't asked for a password and gave me the hash key but now when I again tried to generate the hash key its asking for pass. When I enter "android" as pass, it gives me just DrNQ.
command used:
keytool -exportcert -alias androiddebugkey -keystore "C:\Users\Sam\.android\debug.keystore" | "C:\Openssl\bin\openssl" sha1 -binary | "C:\Openssl\bin\openssl" base64
you are using to much complex trick to get hash key of your application it's having lots of chance to get error.But I have one simple idea to generate your application hash key.
for generating hash key you must need to specify your package name and you are just putting following code while app is lunch and you are getting yout app hash key in Log.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
try {
PackageInfo info = getPackageManager().getPackageInfo(
"com.example.chirag.maptesting",
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));
Toast.makeText(Main2Activity.this, "Hash Key :"+Base64.encodeToString(md.digest(), Base64.DEFAULT), Toast.LENGTH_SHORT).show();
}
} catch (PackageManager.NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
}
Note :
in Above code com.example.chirag.maptesting is my application package name but you must need to paste your package name here.
I hope your are clear with my idea.
Best Luck
Using below code snippet, you can get the hash code.
public static String printKeyHash(Activity context) {
PackageInfo packageInfo;
String key = null;
try {
//getting application package name, as defined in manifest
String packageName = context.getApplicationContext().getPackageName();
//Retriving package info
packageInfo = context.getPackageManager().getPackageInfo(packageName,
PackageManager.GET_SIGNATURES);
Log.e("Package Name=", context.getApplicationContext().getPackageName());
for (Signature signature : packageInfo.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
key = new String(Base64.encode(md.digest(), 0));
// String key = new String(Base64.encodeBytes(md.digest()));
Log.e("Key Hash=", key);
}
} 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());
}
return key;
}

Android FB login app - invalid key hash

I have this problem, I create FB login with FB SDK Android with java in Eclipse. When i debug and run this app in Android Emulator all is correct, i can login i can logout. But when i export app to develop version (.apk), and install it at my phone or at genymotion emulator i geting this errors:
"Invlaid key hash. The key hash xxxxxxxxxxxxxxxxxxx does not match any stored key hashes. Configure your app key hashes at http://developers.facebook.com/apps/72012084472886"
The hash key is create correoct i use this:
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) {
}
Do you have some idea, what is wrong?
Thank you .
When you run an app via ADB Eclipse usually uses a keystore named debug.keystore, this keystore is usually the one you don't use on production apps. When you export your app and create a keystore, the keyhash will change value because you change keystore.
You can implement this code to your app so you can echo the keyhash to you stackrace:
public static void printHashKey(Context pContext) {
try {
PackageInfo info = getPackageInfo(pContext, PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
String hashKey = new String(Base64.encode(md.digest(), 0));
Log.i(TAG, "printHashKey() Hash Key: " + hashKey);
}
} catch (NoSuchAlgorithmException e) {
Log.e(TAG, "printHashKey()", e);
} catch (Exception e) {
Log.e(TAG, "printHashKey()", e);
}
}
The snippet is only suitable for development, not for production!

Categories

Resources