Android Facebook SDK: generate release key hash

隐身守侯 提交于 2019-11-26 19:19:38

问题


I'm building an app in which users can log in with Facebook.

I've created the hash keys like following:

try {
         PackageInfo info = getPackageManager().getPackageInfo(
         "com.app.package",
         PackageManager.GET_SIGNATURES);
         for (Signature signature : info.signatures) {
         MessageDigest md = MessageDigest.getInstance("SHA");
         md.update(signature.toByteArray());
         Log.d("KeyHash", "KeyHash:"+ Base64.encodeToString(md.digest(),
         Base64.DEFAULT));
         Toast.makeText(getApplicationContext(), Base64.encodeToString(md.digest(),
                 Base64.DEFAULT), Toast.LENGTH_LONG).show();
         }
         } catch (NameNotFoundException e) {

         } catch (NoSuchAlgorithmException e) {

         }

In debug mode, everything works well.

When I export the project for release, it gives this error:

"Invalid key hash. The key hash ****************** does not match any stored key hashes"

I paste the key printed in the Facebook Developer dashboard, but the application still gives me that error.

The complete package of my actvity is "com.app.package.views" and I tried to use this package (as Google Play Package Name) in the dashboard, but nothing changed.

How can I solve it? How can I generate the right release key hash?


回答1:


You followed the steps that facebook provides for the creation of a login application?

You need a 'Production keyhash' obtained starting your release keystore:

From comand line:

keytool -exportcert -alias <RELEASE_KEY_ALIAS> -keystore <RELEASE_KEY_PATH> | openssl sha1 -binary | openssl base64

And add this key on facebook app page options.

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




回答2:


I find a solution. for MAC

Use this one to get YOUR_RELEASE_KEY_ALIAS:

keytool -list -keystore /Users/***/Documents/keystore/***.jks

and this one to get your release keyhash:

keytool -exportcert -alias YOUR_RELEASE_KEY_ALIAS -keystore /Users/***/Documents/keystore/***.jks | openssl sha1 -binary | openssl base64

It works for me.




回答3:


The simplest solution.

1) Sign your Apk.

2) Connect your device to machine and Install signed apk on real device.

3) When facebook login is pressed, you will get an error saying "Invalid key hash. The key hash "xxx" does not match any stored key. ..." on your logcat.

4)Copy the logcat Hash Key and put this key to developers.facebook.com/apps/104...../settings/




回答4:


For future reference, if you already have your app on Play Store you can this:

  1. Go to Release Management

  2. select App Signing in Release Management

  3. You can see SHA1 key in hex format App signing certificate.

  4. Copy the SHA1 in hex format and convert it in to base64 format, you can use this link do that without the SHA1: part of the hex.

  5. Go to Facebook developer console and add the key(after convert to base 64) in the settings —> basic –> key hashes.




回答5:


We need to replace the word "openssl" by the path of one file inside the openssl structure.

So, My CMD command is:

C:\Program Files\Java\jre1.8.0_45\bin>keytool -exportcert -alias Informatheus -keystore C:\Users\Atendimento\Dropbox\AndroidKeystore\Keystore | C:\Users\Atendimento\Desktop\openssl\bin\openssl sha1 -binary | C:\Users\Atendimento\Desktop\openssl\bin\openssl base64

It worked.




回答6:


// add this method to your first activity and open log and search for Base64 tag this is the Hashkey i hoop it help

public void getHashkey(){
    try {
        PackageInfo info = getPackageManager().getPackageInfo(getApplicationContext().getPackageName(), PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());

            Log.i("Base64", Base64.encodeToString(md.digest(),Base64.NO_WRAP));
        }
    } catch (PackageManager.NameNotFoundException e) {
        Log.d("Name not found", e.getMessage(), e);

    } catch (NoSuchAlgorithmException e) {
        Log.d("Error", e.getMessage(), e);
    }
}



回答7:


Facebook SDK uses two different keys, one is Debug key which you can use during your development phase and other is Release key which is used once you create a signed application package. Here is a link about how to create debug and release keys.

Developer.Facebook

Also check out this SO post.




回答8:


The simplest way to get hash key released apk is : Get SHA1 key of released apk using following command:

keytool -list -v -keystore keystore_path.jks -alias keystoreAlias

Thn you will get SHA1 key. Copy that key and generate hash key using following site:

Link to get hash key

You will get Output (base64): copy it and use where you want..




回答9:


This one is the easiest way i found so far for generating key hash!

try
{
  PackageInfo info = getPackageManager().getPackageInfo(
      "Your Package Name",
      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 (PackageManager.NameNotFoundException e) {

} catch (NoSuchAlgorithmException e) {

}



回答10:


keytool -exportcert -alias aliasName -keystore C:\my_release_keystroke_info.jks | openssl sha1 -binary | openssl base64


来源:https://stackoverflow.com/questions/27448684/android-facebook-sdk-generate-release-key-hash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!