How to create Android Facebook Key Hash?

2019-01-01 15:06发布

问题:

I do not understand this process at all. I have been able to navigate to the folder containing the keytool in the Java SDK. Although I keep getting the error openssl not recognised as an internal or external command. The problem is even if I can get this to work, what would I do and with what afterwards?

回答1:

Here is what you need to do -

Download openSSl from Code Extract it. create a folder- OpenSSL in C:/ and copy the extracted code here.

detect debug.keystore file path. If u didn\'t find, then do a search in C:/ and use the Path in the command in next step.

detect your 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

it will ask for password, put android that\'s all. u will get a key-hash



回答2:

For Linux and Mac

Open Terminal :

For Debug Build

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

You will find debug.keystore in the \".android\" folder. Copy it and paste onto the desktop and run the above command.

For release Build

keytool -exportcert -alias <aliasName> -keystore <keystoreFilePath> | openssl sha1 -binary | openssl base64

NOTE : Make sure that in both cases it asks for a password. If it does not ask for a password, it means that something is wrong in the command. Password for debug.keystore is \"android\" and for release you have to enter password that you set during create keystore.



回答3:

Please try this:

public static void printHashKey(Context pContext) {
        try {
            PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_SIGNATURES);
            for (Signature signature : info.signatures) {
                MessageDigest md = MessageDigest.getInstance(\"SHA\");
                md.update(signature.toByteArray());
                String hashKey = new String(Base64.encode(md.digest(), 0));
                Log.i(TAG, \"printHashKey() Hash Key: \" + hashKey);
            }
        } catch (NoSuchAlgorithmException e) {
            Log.e(TAG, \"printHashKey()\", e);
        } catch (Exception e) {
            Log.e(TAG, \"printHashKey()\", e);
        }
    }


回答4:

OpenSSL: You have to install that if it doesn\'t come preinstalled with your operating system (e.g. Windows does not have it preinstalled). How to install that depends on your OS (for Windows check the link provided by coder_For_Life22).

The easiest way without fiddling around is copying that openssl.exe binary to your keytool path if you are on Windows. If you don\'t want to do that, you have to add it to your PATH environment variable. Then execute the command provided in the docs.

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

Note that the argument after -keystore points to your debug keystore. This location also depends on your operating system. Should be in one of the following locations:

  • Windows Vista or 7 - C:\\Users\\.android\\debug.keystore
  • Windows XP - C:\\Documents and Settings\\.android\\debug.keystore
  • OS X and Linux - ~/.android/debug.keystore

If you did everything right, you should be prompted for a password. That is android for the debug certificate. If the password is correct the console prints a hash (somewhat random chars and numbers).

Take that and copy it into the android key hash field inside the preferences of your app on facebook. To get there, go to developers.facebook.com/apps, select your app, go to Edit settings and scroll down. After that, wait a few minutes until the changes take effect.



回答5:

to generate your key hash on your local computer, run Java\'s keytool utility (which should be on your console\'s path) against the Android debug keystore. This is, by default, in your home .android directory). On OS X, run:

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

On Windows, use:-

keytool -exportcert -alias androiddebugkey -keystore %HOMEPATH%\\.android\\debug.keystore | openssl sha1 -binary | openssl base64

hope this will help you

Ref - developer facebook site



回答6:

There is a short solution too. Just run this in your app:

FacebookSdk.sdkInitialize(getApplicationContext());
Log.d(\"AppLog\", \"key:\" + FacebookSdk.getApplicationSignature(this));

A longer one that doesn\'t need FB SDK (based on a solution here) :

public static void printHashKey(Context context) {
    try {
        final PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES);
        for (android.content.pm.Signature signature : info.signatures) {
            final MessageDigest md = MessageDigest.getInstance(\"SHA\");
            md.update(signature.toByteArray());
            final String hashKey = new String(Base64.encode(md.digest(), 0));
            Log.i(\"AppLog\", \"key:\" + hashKey + \"=\");
        }
    } catch (Exception e) {
        Log.e(\"AppLog\", \"error:\", e);
    }
}

The result should end with \"=\" .



回答7:

For Windows:

  1. open command prompt and paste below command

keytool -exportcert -alias androiddebugkey -keystore %HOMEPATH%.android\\debug.keystore | openssl sha1 -binary | openssl base64

  1. Enter password : android --> Hit Enter

  2. Copy Generated Hash Key --> Login Facebook with your developer account

  3. Go to your Facebook App --> Settings--> Paste Hash key in \"key hashes\" option -->save changes.

  4. Now Test your android app with Facebook Log-in/Share etc.



回答8:

For someone like me here is complete details (For Windows)

1. Download OpenSSl either 1st or 2nd based on your system 32bit or 64bit .

2. Extract the downloaded zip inside C directory

3. Open the extracted folder up to bin and copy the path ,it should be some thing like C:\\openssl-0.9.8k_X64\\bin\\openssl (add \\openssl at end)

4. (Get the path to the bin folder of Jdk ,if you know how,ignore this ) .

Open android studio ~file~Project Structure(ctrl+alt+shift+s) , select SDK location in left side panel ,copy the JDK location and add /bin to it

So final JDK Location will be like C:\\Program Files\\Android\\Android Studio\\jre\\bin

we are following this method to get Jdk location because you might use embedded jdk like me

\"enter

now you have OpenSSl location & JDK location

5. now we need debug keystore location , for that open C~>Users~>YourUserName~>.android there should be a file name debug.keystore ,now copy the path location ,it should be some thing like

C:\\Users\\Redman\\.android\\debug.keystore

6. now open command prompt and type command

cd YourJDKLocationFromStep4  

in my case

 cd C:\\Program Files\\Android\\Android Studio\\jre\\bin

7. now construct the following command

keytool -exportcert -alias androiddebugkey -keystore YOURKEYSTORELOCATION | YOUROPENSSLLOCATION sha1 -binary | YOUROPENSSLLOCATION base64

in my case the command will look like

keytool -exportcert -alias androiddebugkey -keystore C:\\Users\\Redman\\.android\\debug.keystore | C:\\openssl-0.9.8k_X64\\bin\\openssl sha1 -binary | C:\\openssl-0.9.8k_X64\\bin\\openssl base64

now enter this command in command prompt , if you did ever thing right you will be asked for password (password is android)

Enter keystore password:  android

thats it ,you will be given the Key Hash , just copy it and use it



回答9:

That\'s how I obtained my:

private class SessionStatusCallback implements Session.StatusCallback {
        @Override
        public void call(Session session, SessionState state, Exception exception) {

            if (exception != null) {
                new AlertDialog.Builder(FriendActivity.this)
                        .setTitle(R.string.login_failed_dialog_title)
                        .setMessage(exception.getMessage())
                        .setPositiveButton(R.string.ok_button, null)
                        .show();
            }

So when you re trying to enter without the key, an exception will occur. Facebook put the RIGHT key into this exception. All you need to do is to copy it.



回答10:

For easy vedio tutorial link for the generate KeyHash Here

Download openssl from HERE



回答11:

Since API 26, you can generate your HASH KEYS using the following code in KOTLIN without any need of Facebook SDK.

fun generateSSHKey(context: Context){
    try {
        val info = context.packageManager.getPackageInfo(context.packageName, PackageManager.GET_SIGNATURES)
        for (signature in info.signatures) {
            val md = MessageDigest.getInstance(\"SHA\")
            md.update(signature.toByteArray())
            val hashKey = String(Base64.getEncoder().encode(md.digest()))
            Log.i(\"AppLog\", \"key:$hashKey=\")
        }
    } catch (e: Exception) {
        Log.e(\"AppLog\", \"error:\", e)
    }

}

\"enter



回答12:

Download open ssl:

Then add openssl\\bin to the path system variables:

My computer -> properties -> Advanced configurations -> Advanced -> System variables -> under system variables find path, and add this to its endings: ;yourFullOpenSSLDir\\bin

Now open a command line on your jdk\\bin folder C:\\Program Files\\Java\\jdk1.8.0_40\\bin (on windows hold shift and right click -> open command line here) and use:

keytool -exportcert -alias keystorealias -keystore C:\\yourkeystore\\folder\\keystore.jks | openssl sha1 -binary | openssl base64

And copy the 28 lenght number it generates after giving the password.



回答13:

You can get all your fingerprints from https://console.developers.google.com/projectselector/apis/credentials
And use this Kotlin code to convert it to keyhash:

fun main(args: Array<String>) {
    listOf(\"<your_production_sha1_fingerprint>\",
            \"<your_debug1_sha1_fingerprint>\",
            \"<your_debug2_sha1_fingerprint>\")
            .map { it.split(\":\") }
            .map { it.map { it.toInt(16).toByte() }.toByteArray() }
            .map { String(Base64.getEncoder().encode(it)) }
            .forEach { println(it) }
}


回答14:

I have found the most simple tool and used it many times, It\'s perfectly working. check it out, It will help.

Check this answer for more details.



回答15:

Run either this in your app :

FacebookSdk.sdkInitialize(getApplicationContext());
Log.d(\"AppLog\", \"key:\" + FacebookSdk.getApplicationSignature(this)+\"=\");

Or this:

public static void printHashKey(Context context) {
    try {
        final PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES);
        for (android.content.pm.Signature signature : info.signatures) {
            final MessageDigest md = MessageDigest.getInstance(\"SHA\");
            md.update(signature.toByteArray());
            final String hashKey = new String(Base64.encode(md.digest(), 0));
            Log.i(\"AppLog\", \"key:\" + hashKey + \"=\");
        }
    } catch (Exception e) {
        Log.e(\"AppLog\", \"error:\", e);
    }
}

And then look at the logs.

The result should end with \"=\" .

Solution is based on here and here .



回答16:

https://developers.facebook.com/docs/android/getting-started/

4.19.0 - January 25, 2017

Facebook SDK

Modified

Facebook SDK is now auto initialized when the application starts. In most cases a manual call to FacebookSDK.sdkInitialize() is no longer needed. See upgrade guide for more details.

For Debug

try {
    PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), 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 (NoSuchAlgorithmException e) {
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}


回答17:

I was having the same exact problem, I wasnt being asked for a password, and it seems that I had the wrong path for the keystore file.

In fact, if the keytool doesn\'t find the keystore you have set, it will create one and give you the wrong key since it isn\'t using the correct one.

The general rule is that if you aren\'t being asked for a password then you have the wrong key being generated.



回答18:

You can use this apk

1.first install the app from the Google play store
2.install the above apk
3.launch the apk and input the package name of your app
4.then you will get the hash code you want