Invalid Key hash with Facebook Android SDK

16,553

Solution 1

Try to get the HashKey from here

public static void showHashKey(Context context) {
            try {
                PackageInfo info = context.getPackageManager().getPackageInfo(
                        "com.example.tryitonjewelry", PackageManager.GET_SIGNATURES); //Your            package name here
                for (Signature signature : info.signatures) {
                    MessageDigest md = MessageDigest.getInstance("SHA");
                    md.update(signature.toByteArray());
                    Log.i("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
                    }
            } catch (NameNotFoundException e) {
            } catch (NoSuchAlgorithmException e) {
            }
        }

Solution 2

 try {
            PackageInfo info = getPackageManager().getPackageInfo("your pakage name here", PackageManager.GET_SIGNATURES);
            for (Signature signature : info.signatures) {
                MessageDigest md = MessageDigest.getInstance("SHA");
                md.update(signature.toByteArray());
                Log.e("KeyHash:", "key is: "+Base64.encodeToString(md.digest(), Base64.DEFAULT));

                }
        } catch (NameNotFoundException e) {

            Log.e("error","error name not found");
        } catch (NoSuchAlgorithmException e) {

            Log.e("error","error no algorithm");

        }**strong text**

By using this one u can get ur key hash and then use this one in facebook devloper site.

Share:
16,553

Related videos on Youtube

Blackecho
Author by

Blackecho

Research engineer @ Blue Vision Labs.

Updated on June 04, 2022

Comments

  • Blackecho
    Blackecho almost 2 years

    I'm trying to use Facebook Android SDK to develop a simple app with the Facebook Login Button. But i'm having trouble with Key Hashes. I've created both a debug key and a release key:

    Debug key:

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

    Release key:

    keytool -exportcert -alias <RELEASE_KEY_ALIAS> -keystore <RELEASE_KEY_PATH> | openssl sha1 -binary | openssl base64
    

    And then i copied this key hashes in the Facebook Developers page. When i export the apk and copy it into the device it works well, but when i try to install the app from Eclipse (run as/debug as Android Application) it doesn't work. It seems that the app is using a different key hash from the one i've created with keytool. Anyone knows how to solve this problem?

    • Haresh Chhelana
      Haresh Chhelana almost 10 years
      is it ask you password ?
    • Sathish Kumar
      Sathish Kumar almost 10 years
      You Can Check your Hash key like this 1.Check if your system as 32 bit Operating System you can use OpenSSL 32 bti from here and generate key then compare to programmatic generated hash key if both same it works.
  • Mayank Saini
    Mayank Saini almost 10 years
    If you are giving the same answer which has been posted and then try being supportive to the answer posted rather than posting it again.
  • Bhaskar.Ramagiri
    Bhaskar.Ramagiri almost 10 years
    its ok.actually i didnt sean your post.
  • Blackecho
    Blackecho almost 10 years
    This fixed the issue! Thanks!
  • Unknownweirdo
    Unknownweirdo over 9 years
    This is fantastic. Couldn't work out why the hash we entered was still invalid. Some of the characters were too ambiguous. This allowed for a sweet copy and paste :)
  • swooby
    swooby about 9 years
    I recommend using "context.getPackageName()" to get the package name and make your method more reusable. Also note that if you wrap the Base64.encodeToString in quotes then there are some stray characters that may be desirable to strip.
  • swooby
    swooby about 9 years
    I had the same problem with my keytool signature output not matching what FB expected. Turns out I was impulsively entering my sudo password for the keystore. As soon as I entered no password for the debug.keystore, the keytool output matched what FB expected!
  • Simon
    Simon almost 9 years
    Thanks - I don't know why the keytool method doesn't work but your method solved my issue. Can confirm its working with Android Studios 1.3 preview with Facebook SDK 4.