Facebook Android Generate Key Hash

后端 未结 21 2525
误落风尘
误落风尘 2020-11-22 04:11

Trying to create an android app with Facebook integration, I\'ve gotten to the part in the docs where you have to generate a key hash file, it specifies to run the following

21条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 04:50

    UPDATED ANSWER (Generating through code) Simpler method :

    In my experience, openssl always being troublesome, I tried the second method suggested by facebook. And it's wonderful. This is the best method to get the hash key.

    Second option is to print out the key hash sent to Facebook and use that value. Make the following changes to the onCreate() method in your main activity:

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            try {
                PackageInfo info = getPackageManager().getPackageInfo(
                        "com.facebook.samples.loginhowto", 
                        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) {
    
            }
            ...other operations
    
    }//end of onCreate
    

    Replace com.facebook.samples.loginhowto with your own package name ( package name in Manifest.xml).

    Official link - https://developers.facebook.com/docs/android/login-with-facebook/ ( See the bottom of the page)

    OLD ANSWER (Generating Keyhash using openssl )

    1. to generate signature you need openssl installed on your pc. If you don’t have one download openssl from here
    2. In C: , Create openssl folder
    3. extract the contents of downloaded openssl zip file into openssl folder in C:drive
    4. open Command prompt
    5. move to bin of openssl i.e C:\openssl\bin in command prompt
    6. run the following command to generate your keyhash. While generating hashkey it should ask you password.

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

    NOTE: in the above code note that , you need to give your path to user ( i.e in my case it is C:\Users\Anhsirk , you just need to change this for your user account.

    Give password as android

    . If it don’t ask for password your keystore path is incorrect.

    If everything works fine, it should give you the hashkey below.

    enter image description here

提交回复
热议问题