Invalid key hash after build to apk and how to sol

2019-01-26 01:32发布

I have built a "Log In with Facebook" button for my app, it's working normally in the virtual device, but after I built my app to apk and test it on the real device, there is a problem "Invalid key hash. The key hash lULhSMXXXXXXXXXX does not match any stored key hashes. Configure your app key hashes at http://developers.facebook.com/apps/XXXXXXX. Then I go to developers.facebook.com/apps/XXXXXXX and add the key hash lULhSMXXXXXXXXXXXX to setting and it worked normally. So I have a question, if I install my app in many different devices, how can I make the "Log In with Facebook" button working without have to add a key hash for each device like this. Thank you. This is my code

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    loginButton = (LoginButton) findViewById(R.id.login_button);
    loginButton.setReadPermissions(Arrays.asList("email"));

    callbackManager = CallbackManager.Factory.create();
    loginButton.registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    // App code
                    loginFacebook(loginResult);
                }

                @Override
                public void onCancel() {
                    // App code
                }

                @Override
                public void onError(FacebookException exception) {
                    // App code
                }
            });

private void loginFacebook(final LoginResult loginResult) {
    GraphRequest request = GraphRequest.newMeRequest(
            loginResult.getAccessToken(),
            new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(JSONObject object, GraphResponse response) {

                    String userId = null;
                    String name = null;
                    try {
                        userId = object.getString("id");
                        name = object.getString("name");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }



                    Profile profile = Profile.getCurrentProfile();
                    Log.d("Shreks Fragment onSuccess", "" + profile);

                    Intent i = new Intent(LoginActivity.this, Intent.class);
                    startActivityForResult(i, 0);
                    finish();
                }
            });
    request.executeAsync();

}

1条回答
来,给爷笑一个
2楼-- · 2019-01-26 01:51

We have to add total three key hashes at Facebook developer.

1.using package manager in android app.

  try {
                PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_SIGNATURES);
                for (Signature signature : info.signatures) {
                    MessageDigest md = MessageDigest.getInstance("SHA");
                    md.update(signature.toByteArray());
                    String sign = Base64.encodeToString(md.digest(), Base64.DEFAULT);
                    Log.e("MY KEY HASH:", sign);
                    //textInstructionsOrLink = (TextView)findViewById(R.id.textstring);
                    //textInstructionsOrLink.setText(sign);
                    Toast.makeText(getApplicationContext(), sign, Toast.LENGTH_LONG).show();
                }
            } catch (PackageManager.NameNotFoundException e) {
                Log.d("nope", "nope");
            } catch (NoSuchAlgorithmException e) {
            }

2.Debug key using command line

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

3.Release key using command line

keytool -exportcert -alias app_alias -keystore C:\Users\usename\app_keysrore.jks | C:\openssl-0.9.8k_X64\bin\openssl sha1 -binary | C:\openssl-0.9.8k_X64\bin\openssl base64

please add this all key hashes and check again.

Note :

  1. You have to generate all these keys on the same machine which is used to sign an APK using key store.
  2. We have to add two debug key hashes because key produced by package manager and command line are different.
查看更多
登录 后发表回答