How to Get Key Hashes for Android App Facebook Integration

LinkedIn
Hacker News

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.

-Advertisement-

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.

Related Articles

Join The Discussion

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

    Thank you my friend!!!

  • DhruvKumar

    Good One Man…. Thank u So much… Helpfull tutorial

  • Dimitris Aristeidopoulos

    Thank you so muchhhh…..

  • Rishabh

    Thanks Buddy Really helpful

  • Deepak

    Works Perfectly…
    THNK U . :D :D

  • Shabaz Ahmed

    Thank you for the share :)

  • JoseLuis

    Excelent!! it works!!! Thankss!!!

  • Anmol

    Finally something worked.. Thanks!! :D

  • Manuel Ernesto

    hi! the key hash generate is for Development or Release Key Hash???

    • If you use the code to generate key hash it is always the development debug key.. But if you using commands to generate key hash, you can generate for release key by specifying the path and details of release key.

  • PPV

    is it possible to get multiple hash keys…..

    • No.. keyhash is unique for each keystore

      • ananya agarwal

        We are having this problem from weeks,I did the whole keyhash thing correctly and got the key hash
        but when i try to copy the project in my friend’s eclipse to work together on a same project and using same facebook app it shows invalid key hash.the key w7*********** does not match any store key hash so what’s the problem and how can i resolved this problem

  • Hicham GH

    I use method number 2, could u please tell me where I can find the hach key ??

  • Ananda Karthik

    Works perfect. thank

  • vinoth

    i’ve to upload my photo and video to facebook,twitter,pinterrest,vimeo,instagram from my app android to the social networks is there any sample tutorial as for your tutorial of dropbox it will be very much useful for me because i’m new to android.

  • hasn

    it always gives namenotfoundexception . Dou you have an idea?

    • I think you are going wrong with the package name. Try adding your package name as same to application manifest.xml.

      getPackageManager().getPackageInfo("com.yourapp",
      PackageManager.GET_SIGNATURES);

  • akshay

    thanks

    but this gives you only debug key
    how do we get release key hash

    • What method are you using to get the key hash? 1 or 2 ?

      • Dhruva

        I’m using method 1. How do I get the key hash? Is it same as the one that the apk will be signed of with before publishing to the play store?

  • Tunj

    Great post..u just saved my day.

  • Moshin Khan

    You created some decent points there. I looked online for the problem and found your post most useful. I didnt try the code way but, I did using open SSL.

  • Mathis

    very nice post, i certainly really like this internet site, keep on it

  • Bindesh

    This truly answered my difficulty, thank you!

  • Kiran kumar

    this post is worth a comment. It helped me to integrate my app with facebook.

  • Ajay Bakshi

    This was a definitely nice post.

  • Christen

    I discovered your “How to get Key Hashes for android-facebook app” page and found very useful for me. thanks for sharing

  • Vidya

    Thanks for sharing this. It is really helpful.

  • Sharmila K

    good Post. how do we execute the below piece of code. thanks in advance..

    • You may make a separate method call by puting the above code in a method. Or directly paste inside onCreate(). Run as android app, you will find the hash key code in log.

  • Rakesh

    Very nice blog.

  • Vikranth M

    Nice post.