Saturday, July 14, 2012

Android key hash for Facebook App


Its pretty confusing to get a Key hash value for creating Facebook Apps for Android.

Here are the detailed steps for doing that-

1. We need to download openssl from Google code (64 bit users must download openssl-0.9.8e X64, not the latest version)

2. Extract it.
create a folder- OpenSSL in C:/ and copy the extracted code here.

3. Detect debug.keystore file path.
If we can't find one, then lets do a search in C:/ and we will use the Path in the command in next step.

4. Detect keytool.exe path and go to that dir/ in command prompt and run this command in 1 line-
> keytool -exportcert -alias androiddebugkey -keystore "C:\Documents and Settings\Administrator\.android\debug.keystore" | "C:\OpenSSL\bin\openssl" sha1 -binary |"C:\OpenSSL\bin\openssl" base64

Now, it will ask for password, put android
That's all. It will return a key-hash.

Please post a comment, in case of any issue.

46 comments:

  1. it tells me -exportcert is not recognized as an internal or external command

    ReplyDelete
    Replies
    1. Hi aggrey,
      instead of just 'keytool' in the command; try with the whole path to the keytool.exe. For e.g-
      > c:/path/to/keytool -exportcert -alias androiddebugkey...

      Delete
  2. (Sorry if you can't understand me, i dont speak very well)

    Thanks for this, but i have a problem, i can get the h ask key, and i put that in my aplication in facebook developer, but i make the apk and i install in my cellphone, but i can't login whin facebook (i get a FacebookError), can you help me?

    ReplyDelete
    Replies
    1. uninstall your facebook app on your mobile you'll see that it works

      Delete
  3. Hi! Thanks for exampe.
    But i have a question : where i can get keytool.exe ?

    ReplyDelete
    Replies
    1. Hi Riddler,
      keytool.exe is in the 'bin/' directory within JRE installation folder.
      Something like- C:\Program Files\Java\jre7\bin

      or simply do a search.

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  5. i almost getting all things right but at last step it is surly ask for password for every system..? i have ask for the password as following your steps.....
    please tell me....thanks

    ReplyDelete
    Replies
    1. Ya. I think, u r putting a wrong commend. Follow these steps then-
      1. Please put the entire command in 1 line.
      2. Regenerate 'debug.keystore' file; by just deleting it. Then a rebuild of the debug package will create a new one. Use that.

      Delete
    2. Good job,, i'am succes in http://azhallea.blogspot.com

      Delete
  6. Hello ,
    i m following ur step for my app. but still its giving key mismatch error plz help me out.
    thanks,

    ReplyDelete
    Replies
    1. Hi Gaurav. Check out this post-
      http://stackoverflow.com/questions/7979389/android-invalid-key-and-facebook-connect

      Delete
  7. hola oye me puedes ayudar es que me marca error en sha1-binary
    me dice que es un comando incalido

    ReplyDelete
  8. Its working and asking for password but not provide me any keyhash. what i am doing wrong here

    ReplyDelete
  9. ... is not recognized as an internal or external commands, operable program or batch files

    ReplyDelete
    Replies
    1. ...seems you r not in correct keytool.exe directory. You have to go inside that directory and then run the command.

      so before the command, u need to 'cd' to keytool path

      for e.g-

      c:/Users/UserA>cd c:/path/to/keytool/

      c:/path/to/keytool>keytool /?

      ^ it should return command details, here u can run the above command

      Delete
  10. This comment has been removed by the author.

    ReplyDelete
  11. Thanks man. You saved me a lot of trouble. Keep posting more.

    ReplyDelete
  12. How to get key hash for publishing the application.Please help.

    ReplyDelete
  13. how to get debug.keystore to generate the hash key. Is it the same keystore that we use for phonegap release version?

    I tried generating hash key with that, every time i try to make facebook login with my android device, it says "Invalid android_key parameter, the key *********** does not match any allowed key."

    kindly Help. Thank you

    ReplyDelete
  14. thanks, it helped me in getting hash key.

    ReplyDelete
  15. hello sir,i have tried as per your command mentioned above but it's give me output like.. The system cannot find the path specified. what is wrong with it..

    ReplyDelete
    Replies
    1. so, there should be a path problem. It couldn't find either keytool or openssl or debug.keystore file. 1. for keytool.exe & openssl, simply search & double check the paths, you needto go into the keytool.exe directory & run commends from there. For e.g-

      c:> cd c:/path/to/keytool
      c:/path/to/keytool> RUN THE COMMAND HERE

      Delete
  16. hello i have got hash key using this command keytool -exportcert -alias androiddebugkey -keystore "C:\Users\ViralThakar\.android" | openssl sha1 -binary | openssl base64 but it 's not asked me for password and generate key so, it's right key or not.

    ReplyDelete
  17. This comment has been removed by the author.

    ReplyDelete
  18. Hey, maybke I'm very late with my comment, but the keytool didn't ask for a password. Is this normal? I still got a hash key.
    (Sorry 4 my bad english, it's not my mother tongue)

    ReplyDelete
  19. Hey I able to create hash but thank to u, but where do I search for the .keystore file I have to link it to unity3d ty

    ReplyDelete
  20. hi im new to android, i successfully created the hash key and also registered my project in facebook developer page. for first time it works perfectly. but if i again run my app means new hash key is generated and its showing me invalid hash key

    ReplyDelete
    Replies
    1. does development hash key changes continuosly? how can i manage this scenario. Thank in advance.

      Delete
    2. let follow the following code to generate key hash
      just replace your pakage name in code with your own pkg name
      @Override
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      // Add code to print out the key hash
      try {
      PackageInfo info = getPackageManager().getPackageInfo(
      "com.facebook.samples.hellofacebook",
      PackageManager.GET_SIGNATURES);
      for (Signature signature : info.signatures) {
      MessageDigest md = MessageDigest.getInstance("SHA");
      md.update(signature.toByteArray());
      Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
      }
      } catch (NameNotFoundException e) {

      } catch (NoSuchAlgorithmException e) {

      }

      ...

      Delete
  21. This comment has been removed by the author.

    ReplyDelete
  22. hai,am enter the fellowing steps but it should not ask any password..it show the hask key value.i am confused is it correct or not???

    Microsoft Windows [Version 6.2.9200]
    (c) 2012 Microsoft Corporation. All rights reserved.

    C:\Users\ADMIN>C:\Users\ADMIN\.android
    'C:\Users\ADMIN\.android' is not recognized as an internal or external command,
    operable program or batch file.

    C:\Users\ADMIN>cd C:\Program Files\Java\jdk1.7.0_79\bin

    C:\Program Files\Java\jdk1.7.0_79\bin>keytool -exportcert -alias androiddebugkey
    -keystore "C:\Documents and Settings\Administrator\.android\debug.keystore" | "
    C:\OpenSSL\bin\openssl" sha1 -binary |"C:\OpenSSL\bin\openssl" base64
    Yed1QQfdAJf6lH2TleFWgVbRraY=

    C:\Program Files\Java\jdk1.7.0_79\bin>

    ReplyDelete
  23. C:\Users\Admin\.android>keytool -exportcert -alias androiddebugkey -keystore "C:
    \Users\user.android\debug.keystore" | "C:\openssl\openssl-0.9.8k_X64\bin\openssl
    " sha1 -binary | "C:\openssl\openssl-0.9.8k_X64\bin\openssl" base64
    The filename, directory name, or volume label syntax is incorrect.

    i face this error how to solve it??

    ReplyDelete
  24. thanks its working. But 1 thing I want to know "Are key hash character vary with the project or these are same for all project in 1 PC ?"

    ReplyDelete
  25. This comment has been removed by the author.

    ReplyDelete
  26. This comment has been removed by the author.

    ReplyDelete
  27. Is it possible to use Unix,Facebook - How we can take Release hashkey

    ReplyDelete
  28. It is not asking for password.but asking for an keystore argument. I am having trouble getting the hash key..plz help me out..
    (Command option -keystore needs an argument.)

    ReplyDelete
  29. I got the development hash key by this method thanks. But how do I get the release hash key?
    i used this command
    `keytool -exportcert -alias YOUR_RELEASE_KEY_ALIAS -keystore YOUR_RELEASE_KEY_PATH | openssl sha1 -binary | openssl base64`
    but as i have space in the dir name it shows error, is there any other way to find it? like by Gradle/android/tasks/android/signingReport?

    ReplyDelete