Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

android - Invalid key hash after build to apk and how to solve this for many device?

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();

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:Usersusername.androiddebug.keystore | C:openssl-0.9.8k_X64inopenssl sha1 -binary | C:openssl-0.9.8k_X64inopenssl base64

3.Release key using command line

keytool -exportcert -alias app_alias -keystore C:Usersusenameapp_keysrore.jks | C:openssl-0.9.8k_X64inopenssl sha1 -binary | C:openssl-0.9.8k_X64inopenssl 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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...