Sony Arouje

a programmer's log

Create Facebook Hash Key for Android apps

with 41 comments

Currently doing a small Facebook client for Android. There are several steps we need to do to get the integration working properly. Here I just give a brief of how to create Hash key to update in Facebook. I did a lot of research to get things working, so thought of writing a post that give the details in one place.

Steps to create Hash Key.

  1. Download openssl from Openssl for Windows . I downloaded the Win32 version
  2. Unzip and copy all the files in the bin folder including openssl.exe
  3. Goto to the folder where you installed JDK for me it’s C:\Program Files\Java\jdk1.6.0_21\bin
  4. Paste all the files you copied from Openssl’s bin folder to the Jdk folder.

Now it’s time to run the command to generate the HashKey. You can find more details of Facebook sdk here

keytool -exportcert -alias androiddebugkey  -keystore [Key_store_path] | openssl sha1 -binary | openssl enc -a -e

 

Above command needs the path to your debug keystore. You can find the Keystore path using Eclipse. Goto Window->Preference->Android->Build will open up a window as shown below.

image

You can find the Default debug keystore. Copy the path and update in our command, now the command will be like

keytool -exportcert -alias androiddebugkey  -keystore C:\Users\sarouje\.android\debug.keystore | openssl sha1 -binary | openssl enc -a -e

Fire a command prompt and paste the command and run it. Tool will generate a key for you.

Edit: I was not successful in generating right Key Hash using command prompt or cygWin. My search leads to this post explains how to generate right key for the app. Author also provided the Eclipse project contains the source.

Update Hash Key in Facebook

Goto to your Facebook app page and click Edit settings of your app. Click on the Mobile Native section, page will expand and show the section as shown below

image 

Update the HashKey in the Android Key Hash Textbox and click save changes. We are done, go ahead and run your app and it will work fine. No more Invalid key exception

 

Edit (Jun-24-2014): Nezzar Waleed notified me about one easy solution he found out in stackoverflow, please check it out as well.

Written by Sony Arouje

September 18, 2011 at 3:42 am

41 Responses

Subscribe to comments with RSS.

  1. thank you for the post, you can simply add the path of openssl.exe to the PATH environment variable of your PC instead of copying the content

    • I have used your method, how much time Facebook will take to reflect this change of hash key.

      rooban abraham

      January 4, 2012 at 11:28 am

      • It wont take much time, I got it working within 2 or 3 mins

        Sony Arouje

        January 4, 2012 at 12:26 pm

  2. Great, You save my day. !!
    Appreciate your post !! really Helpful

    Anonymous

    February 1, 2012 at 7:56 pm

    • Good to hear that my post helped you. Appreciate for leaving the comment.

      Sony Arouje

      February 2, 2012 at 11:22 am

  3. i got some error while run this command in cmd prompt can anyone suggest me solution how to get run successfully.

    Error – openssl is not recognised ….keytool is not recognised

    This type of error

    jay

    March 16, 2012 at 7:01 pm

    • Please have a look at davide cesarini’s comment and my reply to his comment. Probably that info will solve your issue.

      Sony Arouje

      March 20, 2012 at 10:59 am

  4. C:\Program Files\Java\jdk1.7.0\bin>keytool -exportcert -alias androiddebugkey -keystore C:\Users\sarouje\.android\debug.keystore | openssl sha1 -binary | openssl enc -a -e it is worked so first find keytool and then insert path thanks for this tutorial!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    chirag

    March 21, 2012 at 4:39 pm

  5. i got message “openssl:Error: ‘shal’ is an invalid command.” also asking me for key store password. what to do now?

    Nitesh

    April 9, 2012 at 4:10 pm

  6. sorry….got it….its sha1 not shal

    Nitesh

    April 9, 2012 at 4:21 pm

  7. It’s much easier to modify Util.java from Facebook project and change:
    private static boolean ENABLE_LOG = false;
    to
    private static boolean ENABLE_LOG = true;

    Then, when running, you’ll get an error in log, like this one:

    Login failed: invalid_key:Android key mismatch. Your key “M76DT4Me9cn2xxxxxxxxxxxxxxxx” does not match the allowed keys specified in your application settings.

    Finally go to Facebook and add this hash: “M76DT4Me9cn2xxxxxxxxxxxxxxxx”. Ah, and change back ENABLE_LOG to false.

    Jorge

    June 13, 2012 at 3:56 am

  8. nice post bro keep it up

    shyam

    July 9, 2012 at 3:51 pm

  9. Please suggest me how to get hash key for facebook app in Mac…

    Parent Child

    September 25, 2012 at 11:17 am

    • Are you not getting the key with the way I explained in the post. If not I don’t how to get it, you may need to try it yourself, I never worked in Mac.

      Sony Arouje

      September 25, 2012 at 11:28 am

  10. thanks a lot …………..

    Anonymous

    November 26, 2012 at 10:37 pm

  11. thanks alot for this post

    Anonymous

    November 26, 2012 at 10:38 pm

  12. its not working actually when i fire command in cmd its showing keytool is not recognized

    girish

    January 10, 2013 at 1:17 pm

    • Hi Girish,
      Check whether the keytool path is added to the Environment Path. Also check whether the keytool exist in you system. A google search will give you all the details you need.

      Regards,
      Sony

      Sony Arouje

      January 10, 2013 at 1:21 pm

  13. Hi Sony, I can’t quite get this to work – do you have any idea what this message could be referring to after I try to execute the command? “Error: Could not determine application home.”
    Kind regards,
    Connor

    Connor

    January 11, 2013 at 6:11 am

    • Hi Connor,
      which command you are trying to execute? I never got these kind of error.

      Regards,
      Sony

      Sony Arouje

      January 11, 2013 at 10:46 am

      • I’ve taken a screenshot of the problem here – I hope it’s a simple fix… Additionally, it still generated a key hash after this error (which I’ve greyed out), but the key hash it gives is incorrect.
        Thanks,
        Connor

        Connor

        January 11, 2013 at 6:19 pm

      • Hi Connor,
        Can you pls see the last section of this post, there I mentioned an Android code that gives the correct hash key. Even commands didn’t work for me either.

        Regards,
        Sony Arouje

        Sony Arouje

        January 14, 2013 at 9:07 pm

  14. hey , its good example. but can you tell me what should be password ? till now i am typing my keystore password. but in some other form i get that it should be only “android” without ” “. & getting http://stackoverflow.com/questions/13894006/android-facebook-sdk-3-0-gives-remote-app-id-does-not-match-stored-id-while-lo same screen in real device not in emulator. please help me.

    chintan khetiya

    March 15, 2013 at 10:50 am

  15. i got the key… and i’ve added in facebook integration settings…. bt still i got misconfigured err!!!! it’ll work fine in 64 bit but not in 32 bit… do you have any solutions..?

    Hiral

    April 2, 2013 at 6:00 pm

    • Hi Hiral,
      Not sure what could be the reason for this error. I haven’t looked at this for a long.

      Regards,
      Sony

      Sony Arouje

      April 4, 2013 at 2:17 pm

  16. dear anyone help me i got error keytool is not recgonize cammand please help i am stuckoff

    fahim

    April 9, 2013 at 4:15 pm

    • Hi Fahim,
      Probably you may need to update the path variable or the keytool not exist in your machine.

      Regards,
      Sony Arouje

      Sony Arouje

      April 9, 2013 at 4:26 pm

      • what do you mean update the path i am using this path
        C:\Users\M faheem\.android\debug.keystore

        fahim

        April 9, 2013 at 4:28 pm

  17. Hello!
    I generated the key hash for android successfully. But i got key hash in unreadable characters like unicode. So I cant use it in the fb developer settings. What should I do for that?

    Anonymous

    May 16, 2013 at 11:55 am

  18. Nice tutrial realy helpfull for me

    Thanks

    Kunal Sharma

    January 28, 2014 at 11:42 am

  19. Thank u! It took me around 3 hours to solve this problem.

    The code to get the hash key from MAC is :

    keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64

    You can get the key from the app through the following code:

    try {
    PackageInfo info = getPackageManager().getPackageInfo(
    “com.example.kuroi”,
    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) {

    }

    The key will be displayed in the LogCat window.

    Rober

    November 9, 2014 at 10:50 pm


Leave a comment