30

i am using android studio to develop an app which uses facebook sdk.

when i used the following command

C:\Program Files\Java\jdk1.7.0_21\bin>keytool -exportcert -alias androiddebugkey
 -keystore "C:\Users\ninad\.android\debug.keystore" | "D:\OpenSSL\bin\openssl" s
ha1 -binary |"D:\OpenSSL\bin\openssl" base64

it returned y5EquINHD6DvwVJFyJTuUEY3NSU=

when using this hash key facebook shows the error

invalid android_key parameter. the key c33Tm0FL_-kxyaPZq1JBLDh767U does not match any allowed key.

Is the hash key needed for facebook different when using eclipse and android studio? which among these is my correct hash key?

I added both the hash Key to my app in facebook. still the app shows invalid android_key parameter.

null pointer
  • 5,874
  • 4
  • 36
  • 66
  • generate hash key by the code http://stackoverflow.com/questions/16767672/key-hash-doesnt-match-while-facebook-login-in-android/16768681#16768681 – Rishi Gautam Jun 06 '13 at 11:35
  • it returns c33Tm0FL_-kxyaPZq1JBLDh767U, i gave this in the facebook page. but it still shows the invalid android_key parameter – null pointer Jun 07 '13 at 12:40
  • https://developers.facebook.com/docs/getting-started/facebook-sdk-for-android/3.0/#sig it is only another way to generate the hash key if u not generate the hash key through the cmd , PackageInfo info = getPackageManager().getPackageInfo( "give the your package name ", like wher u paste the code , i preferred put this code in starting class – Rishi Gautam Jun 07 '13 at 13:22
  • try to replace c33Tm0FL_-kxyaPZq1JBLDh767U with hash key in native-android app. – Hiren Dabhi Jun 11 '13 at 05:53
  • i have added c33Tm0FL_-kxyaPZq1JBLDh767U in my facebook app. but still shows the same error – null pointer Jun 11 '13 at 06:09
  • y5EquINHD6DvwVJFyJTuUEY3NSU is your debug hash key. How did you sign your apk? With debug key your own generated key? – Tarun Jun 11 '13 at 18:44
  • How are you installing your Android app to your device? Sounds like you're obtaining the debug key and at the same time installing the release version on your device. The keys in this case won't match. – DigCamara Jun 11 '13 at 23:21
  • You could list the current alias are inside that file. keytool -list -keystore keystorefile. And then you could use the correct name to get the hash... – superUser Oct 05 '16 at 19:07

10 Answers10

77

WHy dont you try this code and check out the hash key you are using. From the facebook docs:

Besides double checking your key hash generation steps, here is another option that ensures you're using the correct key hash. It involves changing code in one of the sample apps to print the signature sent to Facebook.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Add code to print out the key hash
    try {
        PackageInfo info = getPackageManager().getPackageInfo(
                "your.package", 
                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) {

    }
}
Majid Laissi
  • 19,188
  • 19
  • 68
  • 105
Tarun
  • 13,727
  • 8
  • 42
  • 57
  • 1
    this code returns c33Tm0FL_-kxyaPZq1JBLDh767U, i gave this in the facebook page. but it still shows the invalid android_key parameter – null pointer Jun 12 '13 at 05:29
  • c33Tm0FL_-kxyaPZq1JBLDh767U definitely doesn't appear to be a valid key. – Tarun Jun 12 '13 at 09:38
8

Here is the link to create facebook hash key:

public class FacebookHashKeyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_facebook_hash_key);
        try {
            PackageInfo info = getPackageManager().getPackageInfo("your.package.name", 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) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
}

Code taken from here.

Sufian
  • 6,405
  • 16
  • 66
  • 120
Vijay Kumar
  • 3,325
  • 2
  • 15
  • 13
6

just run it in oncreate method

private void printHashKey() {

    try {
        PackageInfo info = getPackageManager().getPackageInfo(
                "-----------PUT YOUR PACKAGE NAME ------------",
                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 (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

}
Amir
  • 8,821
  • 7
  • 44
  • 48
1
  1. generate sha1, and copy it
  2. go this website http://tomeko.net/online_tools/hex_to_base64.php
  3. copy Output (base64)

enter image description here

Rahman Rezaee
  • 1,943
  • 16
  • 24
0

Assuming you did copy-paste from openssl output to facebook app settings so that there are no confusions regarding similar-looking characters O0 or 1l| or G6...

I recently put a diagnostic program on Play Store that shows the key hash as-installed for any app on your phone. Search for "GetKeyHash". It shows you all the apps on your device sorted by when they were last updated and you can expand any of them to see the key hash(es), package name, and valid Activity names. The correct GetKeyHash app does not request or use any special permissions and Android should say so when you install.

To run on a simulator, this is also available at http://rightparen.com/android/GetKeyHash.apk.

Note that if you build a debug version of your app in an IDE, the key hash will be different than the retail version you ship to Play Store. You can add multiple key-hashes for an app in the Facebook developer console to allow for debug keys as well as your retail key.

If this yields the same result, there could be a bug. As a potential workaround for a debug key only, you can back up ~/.android/debug.keystore and delete that file. The next time you build, the android system will generate a new key that may not trigger the bug. At this point you will need to uninstall the app from your device or you will get an error on install about the apk needing to be signed with the same certificate. Then you can use GetKeyHash as above or any of the standard methods to get the new key hash and add it to your app on facebook.

rightparen
  • 1,693
  • 10
  • 15
  • I got the following error when trying to install the apk: W/PackageParser: Unable to read AndroidManifest.xml of /data/local/tmp/GetKeyHash.apk java.io.FileNotFoundException: AndroidManifest.xml – Kamen Dobrev Jan 20 '16 at 18:41
  • 1
    @KamenDobrev thanks for letting me know. The other answers here will probably do what you want. Fwiw, I had let my developer agreement expire so Google Play was telling me the app had been removed. I just updated it though, so with any luck you should be able to install it tomorrow in case that proves useful. Sorry for the trouble. – rightparen Jan 22 '16 at 01:54
0

You could list the current alias are inside that file. And then you could use the correct name to get the hash... . Leave the password in blank, just press enter key...

keytool -list -keystore keystorefile

*****************  WARNING WARNING WARNING  *****************
* La integridad de la información almacenada en el almacén de claves  *
* NO se ha comprobado.  Para comprobar dicha integridad, *
* debe proporcionar la contraseña del almacén de claves.                  *
*****************  WARNING WARNING WARNING  *****************

Tipo de Almacén de Claves: JKS
Proveedor de Almacén de Claves: SUN

Su almacén de claves contiene 1 entrada

androiddebugkey, Sep 22, 2016, PrivateKeyEntry, 
Huella Digital de Certificado (SHA1): 3B:8F:DB:46:A0:B7:2E:37:E1:78:41:10:BD:D0:8A:FF:63:E0:94:22

As you can see.. theres 1 key with name androiddebugkey... probably in your case you dona have that... o didnt write the specific when you ran:

{C:\Program Files\Java\jdk1.7.0_21\bin>keytool -exportcert -alias androiddebugkey
 -keystore "C:\Users\ninad\.android\debug.keystore" | "D:\OpenSSL\bin\openssl" s
ha1 -binary |"D:\OpenSSL\bin\openssl" base64
}

wich will return anything it wants... in this case.

y5EquINHD6DvwVJFyJTuUEY3NSU=
superUser
  • 1,032
  • 12
  • 9
0

I personally like to generate my Hash inside the application build.gradle file.

This build.gradle function will generate the hash on Windows (change C:\\OpenSSL-Win64\\bin\\openssl to your OpenSSL binary location):

def getFacebookHash = { ->
    try {
        def stdOut = new ByteArrayOutputStream()
        def stdIn = new ByteArrayInputStream()
        exec {

            commandLine "keytool", "-exportcert", "-alias", KEY_ALIAS, "-keystore", STORE_FILE, "-storepass", STORE_PASSWORD
            standardOutput = stdOut
        }
        stdIn = new ByteArrayInputStream(stdOut.toByteArray())
        stdOut = new ByteArrayOutputStream()
        exec {
            standardInput = stdIn
            commandLine "C:\\OpenSSL-Win64\\bin\\openssl", "sha1", "-binary"
            standardOutput = stdOut
        }
        stdIn = new ByteArrayInputStream(stdOut.toByteArray())
        stdOut = new ByteArrayOutputStream()
        exec {
            standardInput = stdIn
            commandLine "C:\\OpenSSL-Win64\\bin\\openssl", "base64"
            standardOutput = stdOut
        }
        def hashString = stdOut.toString().trim()
        return hashString
    }
    catch (error) {
        return error.toString();
    }
}

You can then print the result anywhere in your Gradle script.

println "Facebook Release Key Hash: " + getFacebookHash()

I keep the necessary details in gradle.properties:

STORE_FILE=C:/Keystore.jks
STORE_PASSWORD=password
KEY_ALIAS=keystoreAlias
KEY_PASSWORD=password
Tim Krins
  • 3,134
  • 1
  • 23
  • 25
0

Add this line to mainActivity.java

  @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

 try {
 PackageInfo info = getPackageManager().getPackageInfo("**Package name**", 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 (PackageManager.NameNotFoundException e) {
 Log.e("KeyHash:", e.toString());
 } catch (NoSuchAlgorithmException e) {
 Log.e("KeyHash:", e.toString());
 }

 }
Manan Gadhiya
  • 480
  • 4
  • 6
0

Generate sha1 key for android project

Go JDK bin dir then open cmd

C:\Program Files\Java\jdk-9.0.1\bin>keytool -exportcert -alias androiddebugkey -keystore "C:\Users\pcUser\.android\debug.keystore" | "C:\Users\pcUser\Desktop\openssl-0.9.8d_X64\bin\openssl" sha1 -binary | "C:\Users\pcUser\Desktop\openssl-0.9.8d_X64\bin\openssl" base64

Download OpenSSL (if needed)

https://code.google.com/archive/p/openssl-for-windows/downloads

Shomu
  • 2,734
  • 24
  • 32
-2

For debugging, if using Eclipse, you will want to generate this Key Hash using the Android debug key. When you are ready to publish your app, you will need to generate a Key Hash for your signing keys and update this value in Facebook before your signed app will work.

Have a look at below links

Android Facebook SDK: How to generate a non-debug hash key?

Do I need to add new key hash to Facebook app setting every time I change computer?

Community
  • 1
  • 1
Phantomcho
  • 302
  • 1
  • 6