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.
Nice post.
Very nice blog.
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.
Thanks for sharing this. It is really helpful.
I discovered your “How to get Key Hashes for android-facebook app” page and found very useful for me. thanks for sharing
This was a definitely nice post.
this post is worth a comment. It helped me to integrate my app with facebook.
This truly answered my difficulty, thank you!
very nice post, i certainly really like this internet site, keep on it
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.
Great post..u just saved my day.
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 ?
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?
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);
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.
Yes, you can find dropbox tutorial here
http://stacktips.com/android/share-file-to-dropbox-in-android-example
Works perfect. thank
I use method number 2, could u please tell me where I can find the hach key ??
is it possible to get multiple hash keys…..
No.. keyhash is unique for each keystore
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
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.
Finally something worked.. Thanks!! 😀
Excelent!! it works!!! Thankss!!!
Thank you for the share 🙂
Works Perfectly…
THNK U . 😀 😀
Thanks Buddy Really helpful
Thank you so muchhhh…..
Good One Man…. Thank u So much… Helpfull tutorial
Thank you my friend!!!
this is for windows please tell how to do it in ubuntu..
sir…i tried this code…but toByteArray function cant resoleve
import Security class from android.content.pm and not from java.security
what to do…??