简体   繁体   中英

Facebook hash key issue

I am trying to access Facebook info of loggedIn user. For that I read through the various docs and found that hash key configuration is required for this on developer section of facebook where application is created and generating Facebook App-Id. I tried to generate hash key for filling into Native Android App section on facebook developer portal. I gone through many links for this like Here

But hash key is not working at all. Sometimes it says no hash is match or sometime it just promt a facebook dialog and disappears.

But still it's not working in my Device. It's working fine on Emulator. I am using Mac system, is this any issue?

Can anybody guide me if I am missing something? Do I need to edit more things in Application setting on facebook developer portal?

This is because the device is creating a new key hash which is not in your app settings. So you have to update the keyhash accordingly. Use this in onCreate()

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 (NameNotFoundException e) {

        } catch (NoSuchAlgorithmException e) {

        }

This has been an issue for many people. Alternatively from generating the hash from commandline, you can generate it from code, and then print it to the log so you can copy it from there.

The following code does that. You can place it in your activity onCreate() for instance, and remove it once you have the hash. Obviously you need to change the package name to your own.

try {
    PackageInfo info = getPackageManager().getPackageInfo(
            "com.facebook.samples.loginhowto", 
             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) {
}

This answer nicely describes how to do the same from commandline, if you don't want to use the code. Both should have the same result.

Try this when session open

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 (NameNotFoundException e) {

        } catch (NoSuchAlgorithmException e) {

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM