Tutorials Android How to Get Key Hashes for Android App Facebook Integration

How to Get Key Hashes for Android App Facebook Integration

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.

Total
12
Shares
guest
38 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Vikranth M
Vikranth M
9 years ago

Nice post.

Rakesh
Rakesh
9 years ago

Very nice blog.

Sharmila K
Sharmila K
9 years ago

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

user
user
9 years ago
Reply to  Sharmila K

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.

Vidya
Vidya
9 years ago

Thanks for sharing this. It is really helpful.

Christen
Christen
9 years ago

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

Ajay Bakshi
Ajay Bakshi
9 years ago

This was a definitely nice post.

Kiran kumar
Kiran kumar
9 years ago

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

Bindesh
Bindesh
9 years ago

This truly answered my difficulty, thank you!

Mathis
Mathis
9 years ago

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

Moshin Khan
Moshin Khan
9 years ago

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.

Tunj
Tunj
9 years ago

Great post..u just saved my day.

akshay
akshay
9 years ago

thanks

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

user
user
9 years ago
Reply to  akshay

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

Dhruva
Dhruva
8 years ago
Reply to  user

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?

hasn
hasn
9 years ago

it always gives namenotfoundexception . Dou you have an idea?

javatechig
javatechig
9 years ago
Reply to  hasn

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

vinoth
vinoth
8 years ago

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.

javatechig
javatechig
8 years ago
Reply to  vinoth
Ananda Karthik
Ananda Karthik
8 years ago

Works perfect. thank

Hicham GH
Hicham GH
7 years ago

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

PPV
PPV
7 years ago

is it possible to get multiple hash keys…..

JavaTechig
JavaTechig
7 years ago
Reply to  PPV

No.. keyhash is unique for each keystore

ananya agarwal
ananya agarwal
6 years ago
Reply to  JavaTechig

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

Manuel Ernesto
Manuel Ernesto
7 years ago

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

JavaTechig
JavaTechig
7 years ago
Reply to  Manuel Ernesto

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.

Anmol
Anmol
7 years ago

Finally something worked.. Thanks!! 😀

JoseLuis
JoseLuis
7 years ago

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

Shabaz Ahmed
Shabaz Ahmed
7 years ago

Thank you for the share 🙂

Deepak
Deepak
7 years ago

Works Perfectly…
THNK U . 😀 😀

Rishabh
Rishabh
7 years ago

Thanks Buddy Really helpful

Dimitris Aristeidopoulos
Dimitris Aristeidopoulos
6 years ago

Thank you so muchhhh…..

DhruvKumar
DhruvKumar
6 years ago

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

Vanderson Silva
Vanderson Silva
6 years ago

Thank you my friend!!!

Rohan
Rohan
5 years ago

this is for windows please tell how to do it in ubuntu..

Ritika Gupta
Ritika Gupta
5 years ago

sir…i tried this code…but toByteArray function cant resoleve

Ansuman
Ansuman
4 years ago
Reply to  Ritika Gupta

import Security class from android.content.pm and not from java.security

Ritika Gupta
Ritika Gupta
5 years ago

what to do…??

Previous Post
Android Tutorials

How to Display HTML in Android TextView

Next Post
Android Tutorials

Handler and AsyncTask in Android

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
38
0
Would love your thoughts, please comment.x
()
x