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:33

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 :)

查看更多
情到深处是孤独
3楼-- · 2018-12-31 04:34

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

查看更多
永恒的永恒
4楼-- · 2018-12-31 04:34

1) Create a key to sign your application, and remember the alias.

2) Install OpenSSL.

3) Put the bin folder of OpenSSL in your path.

4) Follow the steps mentioned under "Setup Single Sign-On" on the FB-Android-SDK page, and generate your Hash Key. Make sure you put the correct alias and keystore file name.

5) Create an application on Facebok, and under Mobile Devices tab, enter this Hash Key.

查看更多
千与千寻千般痛.
5楼-- · 2018-12-31 04:36

Official Documentation on facebook developer site:

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

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

    }
查看更多
梦寄多情
6楼-- · 2018-12-31 04:36
keytool -exportcert -alias androiddebugkey -keystore       C:\Users\pravin\.android\debug.keystore | "H:\OpenSSL\bin\openssl" sha1 -binary | "H:\OpenSSL\bin\openssl" base64

This worked for me ...

Steps:

1) Open command line go to - > java Keytool..... for me C:\Program Files\Java\JDK1.7\bin
2) Download OpenSSL from google
3) paste this with changing your paths -
   keytool -exportcert -alias androiddebugkey -keystore C:\Users\pravin\.android\debug.keystore | "H:\OpenSSL\bin\openssl" sha1 -binary | "H:\OpenSSL\bin\openssl" base64 

    ....................   give proper debug.keystore path and openSSL path .. 

4) Finley it may be ask u password .. so give password -> android   ...
5) you will get 28 characters that will be your has key
查看更多
泪湿衣
7楼-- · 2018-12-31 04:36

The simplest solution:

  1. Don't add the hash key, implement everything else
  2. When facebook login is pressed, you will get an error saying "Invalid key hash. The key hash "xxx" does not match any stored key. ..."
  3. Open the facebook app dashboard and add the hash "xxx=" ("xxx" hash from the error + "=" sign)
查看更多
登录 后发表回答