This section of the tutorial explains you how to get Key Hashes for Android Facebook app. Facebook SDK integration to android require an key hash configuration.

While integrating Facebook SDK, we need to configure Facebook API console with Key Hash. If key hash is missing you will get App mis-configured error in Facebook dashboard.

Download the facebook-android-sdk and configured a new app. In the application settings enable “Native android app” and enable for Facebook login and deep linking. It asks for key hashes, and I’m stuck. After spending a generous amount of time, I have found two solutions to get the key hashes.

1. Using OpenSSL and command prompt

  • Download openssl from Google code (If you have a 64 bit machine you must download openssl-0.9.8e X64 not the latest version)
  • Extract it. Create a folder- OpenSSL in C: / and copy all files here
  • Find “debug.keystore” file path. Most likely it will be inside “C:\Users\\.android” folder. However, if you still don’t find then perform a search. I am sure you are lucky enough to get it.
  • Find keytool.exe path. It will be inside your java/bin directory. In my system it is under “C:\Program Files\Java\jdk1.6.0_30\bin”
  • Open command prompt (Run-> cmd->start) and go to java /bin folder (cd “C:\Program Files\Java\jdk1.6.0_30\bin” command will do it for you)
  • Now you run the below command.
C:\\Program Files\\Java\\jdk1.6.0_30\\bin>keytool -exportcert -alias androiddebugkey -keystore "C:\\Users\\.android\\debug.keystore" | "C:\\OpenSSL\\bin\\openssl" sha1 -binary |"C:\\OpenSSL\bin\\openssl" base64
  • Provide password (android), as when you are prompted. You are done. It will generate you the key-hash

2. Using a method call from android code

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

You may use any of the above methods to generate the key hash for your facebook app.

This article is about: Facebook oAuth, OpenSSL,

Nilanchala

A blogger, a bit of tech freak and a software developer. He is a thought leader in the fusion of design and mobile technologies. He is the author of Xamarin Mobile Application Development for Android Book (goo.gl/qUZ0XV3), DZone MVB and founder of stacktips.com.

Discussions

Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.