Key hash for Android-Facebook app

后端 未结 30 2687
误落风尘
误落风尘 2020-11-22 01:17

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,

相关标签:
30条回答
  • 2020-11-22 01:48

    The instructions currently in Facebook's Android Tutorial do not work well under Windows. Their example shows how to pipe the keytool output to openssl but if you try this under Windows the output is not valid for some reason. I found that I had to use intermediary files to get it to work properly. Here are the steps that worked for me:

    Start by downloading openssl for Windows from Google.

    C:\Users\Me>keytool -exportcert -alias my_key -keystore my.keystore -storepass PASSWORD > mycert.bin
    
    C:\Users\Me>openssl sha1 -binary mycert.bin > sha1.bin
    
    C:\Users\Me>openssl base64 -in sha1.bin -out base64.txt
    

    After running these commands the valid hash is stored in the file base64.txt. Copy and paste this to your app settings on Facebook.

    0 讨论(0)
  • 2020-11-22 01:49

    I have done by this way for Linux OS & Windows OS:

    Linux:

    • Download Openssl
    • Open terminal
    • keytool -exportcert -alias **myaliasname** -keystore **/home/comp-1/Desktop/mykeystore.jks** | openssl sha1 -binary | openssl base64

    Kindly change Alias Name and Keystore with it's path as your requirement.

    Terminal would ask for Password of Keystore. You have to provide password for the same Keystore.

    So finally you would get the Release Hashkey.

    Windows:

    Steps for Release Hashkey:

    • Download Openssl (Download from here), I have downloaded for 64 bit OS, you can find more here
    • Extract downloaded zip file to C:\ drive only
    • Open command prompt
    • keytool -exportcert -alias **myaliasname** -keystore **"C:\Users\hiren.patel\Desktop\mykeystore.jks"** | "C:\openssl-0.9.8e_X64\bin\openssl.exe" sha1 -binary | "C:\openssl-0.9.8e_X64\bin\openssl.exe" base64

    Kindly change Alias Name and Keystore with it's path as your requirement.

    Note:

    Please put your details where I have marked between ** **.

    Terminal would ask for Password of Keystore. You have to provide password for the same Keystore.

    So finally you would get the Release Hashkey.

    Done

    0 讨论(0)
  • 2020-11-22 01:49

    As answered on a similar issue i found this to be working for me:

    • Copy the apkname.apk file you want to know the hash of to the 'Java\jdk1.7.0_79\bin' folder
    • Run this command keytool -list -printcert -jarfile apkname.apk
    • Copy the SHA1 value and convert it using this site
    • Use the converted Keyhash value (ex. zaHqo1xcaPv6CmvlWnJk3SaNRIQ=)
    0 讨论(0)
  • 2020-11-22 01:50

    The best approach is to use the following code:

    private void getHashKey(String pkgName)
    {
        try
        {
            PackageInfo info = getPackageManager().getPackageInfo(pkgName, PackageManager.GET_SIGNATURES);
            for (Signature signature : info.signatures)
            {
                MessageDigest md = MessageDigest.getInstance("SHA");
                md.update(signature.toByteArray());
                String hashKey = Base64.encodeBytes(md.digest());
                _hashKey_et.setText(hashKey);
                Log.i("KeyTool", pkgName + " -> hashKey = " + hashKey);
            }
        }
        catch (NameNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        }
    }
    

    But I was so frustrating with the fact that there is no simple tool to generate the HashKey for the Facebook app. Each time I had to play with Openssl and Keytool or to use a code to get the hash from signature ...

    So I wrote a simple KeyGenTool that will do that work for you: -> KeyGenTool on Google Play <-

    Enjoy :)

    0 讨论(0)
  • 2020-11-22 01:52

    There are two methods available complex one and the easy one

    Methods One :(little Complex)

    first of all you have to download ssl 64bit or 32bit accordingly, remember to download the file with name containing e after version code openssl-0.9.8e_X64.zip OR openssl-0.9.8e_WIN32.zip not with the k after the version code,

    and place in AndroidStudio/jre/bin directory, if you dont know where to place, you can find this directory by right clicking on the android studio shortcut as:

    now you have managed two required things in one place, but still you have to find the path for your debug.keystore, that is always can be found in the "C:\Users\yourusernamehere\.android\debug.keystore",

    NOTE If your app is published already, or about to publish then use your publishing signing keystore, if and only if your are testing in development mode than you can use debug,keysotre

    As everything is setup, let arrange the command you wanted to execute for the hash key generation in base64 format, and you command will look like this

    keytool.exe -exportcert -alias androiddebugkey -keystore "C:\Users\ayyaz talat\.android\debug.keystore" | "D:\Program Files\Android\Android Studio\jre\bin\openssl\bin\openssl.exe" sha1 -binary |"D:\Program Files\Android\Android Studio\jre\bin\openssl\bin\openssl.exe" base64
    

    it will promt you to enter a password for the debug.keystore, which is android by default. if you are using your own key than the password will be also yours. the output will look like this if everything goes well as expected, hope it may help

    Second Method (Respectively easy one)

    if you dont want to go throught all the above procedure, then just use the following method to log the haskey:

     private void printKeyHash() {
            try {
                PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_SIGNATURES);
                for (Signature signature : info.signatures) {
                    MessageDigest md = MessageDigest.getInstance("SHA1");
                    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());
            }
        }
    

    output:

    0 讨论(0)
  • 2020-11-22 01:53

    The simplest solution I have found is this:

    • Open up Log Cat
    • Try and access Facebook with the Android SDK
    • Look for the line in the log that looks like this:

      04-24 01:14:08.605: I/System.out(31395): invalid_key:Android key mismatch. 
      Your key "abcdefgHIJKLMN+OPqrstuvwzyz" does not match the allowed keys specified in your
      application settings. Check your application settings at 
      http://www.facebook.com/developers
      
    • Copy "abcdefgHIJKLMN+OPqrstuvwzyz" and paste it into the Facebook Android Key Hash area.

    0 讨论(0)
提交回复
热议问题