Key hash for Android-Facebook app

2018-12-31 03:43发布

I'm working on an Android app, in which I want to integrate a Facebook posting feature. I downloaded the Facebook-Android SDK, and I got the readme.md (text file) in there, in which it is mentioned to generate the key hash for Android. How do I generate it?

29条回答
萌妹纸的霸气范
2楼-- · 2018-12-31 04:21
  • download openSSL for windows in here you can find 64bit and 32bit here

  • extract the downloaded file

  • create folder name openSSL in C drive
  • copy all the extracted items in to openSSL folder (bin,include,lib,openssl.cnf)
  • get android debug keystore, default location will be

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

  • now get your command prompt and paste this code

keytool -exportcert -alias androiddebugkey -keystore C:\Users\username.android\debug.keystore | "C:\openSSL\bin\openssl" sha1 -binary | "C:\openSSL\bin\openssl" base64

  • hit enter and you will get the 28 digit keycode
查看更多
笑指拈花
3楼-- · 2018-12-31 04:22

This is what is given at the official page of Facebook:

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

Let me break this command into fragments.

  1. Look for "keytool.exe". You can search that on the C: drive. You can find it in "java jdk" or "java jre". If you have installed multiple versions, choose any.

  2. Open a CMD prompt and go to the above directory where you found "keytool.exe".

    Clip the "exe`" and paste the above command provided on the Facebook page.

  3. You will get an error on entering this that OpenSSL is not recognized as in input output command. Solution : Download "Openssl" from OpenSSL (if you have a 64-bit machine you must download openssl-0.9.8e X64). Extract and save it anywhere... I saved it on the C: drive in the OpenSSl folder

  4. Replace the openssl in the above command in which you was getting an error of OpenSSL with "C:\OpenSSL\bin\openssl" at both the places after the pipe, "|".

  5. If prompted for a password, enter android.

And you will get your hash key. For further steps, refer again to the Facebook page.

查看更多
墨雨无痕
4楼-- · 2018-12-31 04:22

For an Android application

This code is used to get the hash key in your Android application for Facebook integration. I have tested all devices and it's working. Only change the package name of this code:

private void facebookHashKey() {

    try {
        PackageInfo info = getPackageManager().getPackageInfo("com.app.helpcove", PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            String hashCode  = Base64.encodeToString(md.digest(), Base64.DEFAULT);
            System.out.println("Print the hashKey for Facebook :"+hashCode);
            Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
    } catch (NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }
}
查看更多
不流泪的眼
5楼-- · 2018-12-31 04:23

You need to create a keystore by the keytool for signed apps for android like the procedure described in Android Site and then you have to install cygwin and then you need to install openssl from google code then just execute the following command and you will get the hash key for android and then put that hash key into the facebook application you created. And then you can access the facebook application through the Android Application for posting wall ("publish_stream") could be an example.

$ keytool -exportcert -alias alias_name -keystore sample_keystore.keystore | openssl sha1 -binary | openssl base64

You need to execute the above command from cygwin.

查看更多
萌妹纸的霸气范
6楼-- · 2018-12-31 04:25

You can use this code in any activity. It will log the hashkey in the logcat, which is the debug key. This is easy, and it's a relief than using SSL.

PackageInfo info;
try {
    info = getPackageManager().getPackageInfo("com.you.name", PackageManager.GET_SIGNATURES);
    for (Signature signature : info.signatures) {
        MessageDigest md;
        md = MessageDigest.getInstance("SHA");
        md.update(signature.toByteArray());
        String something = new String(Base64.encode(md.digest(), 0));
        //String something = new String(Base64.encodeBytes(md.digest()));
        Log.e("hash key", something);
    }
} catch (NameNotFoundException e1) {
    Log.e("name not found", e1.toString());
} catch (NoSuchAlgorithmException e) {
    Log.e("no such an algorithm", e.toString());
} catch (Exception e) {
    Log.e("exception", e.toString());
}

You can delete the code after knowing the key ;)

查看更多
不流泪的眼
7楼-- · 2018-12-31 04:25

I did a small mistake that should be kept in mind. If you are using your keystore then give your alias name, not androiddebugkey...

I solved my problem. Now if Facebook is there installed in my device, then still my app is getting data on the Facebook login integration. Just only care about your hash key.

Please see below.

C:\Program Files\Java\jdk1.6.0_45\bin>keytool -exportcert -alias here your alias name  -keystore "G:\yourkeystorename.keystore" |"G:\ssl\bin\openssl" sha1 -binary | "G:\ssl\bin\openssl" base64

Then press Enter - it will ask you for the password and then enter your keystore password, not Android.

Cool.

查看更多
登录 后发表回答