Post updated – generate the Facebook hash key even easier!

You are trying to connect your Android app to Facebook and you just got the following message “App is misconfigured for Facebook login. Press Okay to go back to the application without connecting it to Facebook.”. Don’t worry, you can solve this problem pretty easy with a bit of help.

First of all, you have probably messed up while creating your key hash key, that is the most common mistake. Forget about creating the key via cmd, it is not as reliable as the following method which is almost foolproof. First of all, you need a project that has Facebook integrated and is sure to work. Your best bet is the official Facebook project named Hackbook. Go to and download it from github. Unpack it, import it to Eclipse, reference Facebook SDK and look for Hackbook.java. Add a few newlines to onCreate method and paste the following code:

PackageInfo info;
try {
info = getPackageManager().getPackageInfo("com.facebook.android", 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());
}

Now you need to import all of the missing references.Before running the project, you need to fix a few things.Take a peek at Facebook developer page. There you need to edit your app and copy your APP ID. In Hackbook.java look for APP_ID and replace the string with your own APP ID. Hopefully you will now be able to run the sample project. It will generate and display the correct hash key in the LogCat. Congrats, you have just successfully created the correct key hash – for Hackbook. The output will look something like this:

App is misconfigured for Facebook login

You need to transfer this code to your own project, but make sure you make one very important change. You need to alter the upper code a bit – replace com.facebook.android with your project details. If you do not know where to find them, look in Manifest for the line that contains the package name (example¬†package=”com.example.val202″). You need to paste this string value to the upper code. After that run your project and get the key hash. Copy and paste it into the developer page for your app in the Key Hashes field. You should be good to go!

If you want to make sure that Hackbook project was set up correctly, you can test the key.. On the developer page, you need to change two things. For Package Name put com.facebook.android and for Class Name type in com.facebook.android.Hackbook. Wait a minute or two for the changes to come in to effect. After that, you can rebuild the Hackbook project and run it. When you click the facebook button in the main activity, you will not see the error anymore.

When you want to start fixing your own project, do not forget to change the package name and class name!