android facebook integration invalid key hash

2018-12-31 23:30发布

hello all in one of my app I need to get data of fb... I am doing that..

I have created app ID it log in successfully but after log out i Log In then it gives me

screen-shot of invalid key hash error facebook

What is wrong I am doing? Please suggest I am using Facebook sdk... I have installed Facebook in my phone... this running good in emulator that does not have inbuilt Facebook application installed

this is my code

 if (FB_APP_ID == null) {
            Builder alertBuilder = new Builder(this);
            alertBuilder.setTitle("Warning");
            alertBuilder.setMessage("A Facebook Applicaton ID must be " +
                    "specified before running this example: see App.java");
            alertBuilder.create().show();
        }

        // Initialize the dispatcher
        Dispatcher dispatcher = new Dispatcher(this);
        dispatcher.addHandler("login", LoginHandler.class);
        dispatcher.addHandler("stream", StreamHandler.class);
        dispatcher.addHandler("logout", LogoutHandler.class);

        // If a session already exists, render the stream page
        // immediately. Otherwise, render the login page.
        Session session = Session.restore(this);
        if (session != null) {
            dispatcher.runHandler("stream");
        } else {
            dispatcher.runHandler("login");
        }

19条回答
孤独寂梦人
2楼-- · 2019-01-01 00:02

I got the same problem.I was sure that it was due to very small fault and yes it was!!!! I found the solution.

When generating debug hash key in my computer, I entered the password of my system.But the password should be the following -
Enter keystore password: "android"
This was the only problem in my case.

----- For generating Debug key hash, use this command -

keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64

Enter keystore password: 'android'

----- To Generate Release key hash, use this command -

keytool -exportcert -alias "alias of keystore" -keystore "Your path to the keystore when signing app" | openssl sha1 -binary | openssl base64

Provide your keystore password after executing this command.

查看更多
一个人的天荒地老
3楼-- · 2019-01-01 00:02

I had the same problem when I was debugging my app. I've rewrited the hash that you have crossed out in the attached image (the one that Facebook says is invalid) and added it in Facebook's developers console to key hashes. Just be careful on typos.

This solution is more like easy work-around than a proper solution.

查看更多
只若初见
4楼-- · 2019-01-01 00:03

You must create two key hashes one for Debug and one for Release.

For Debug key hash:

On OS X, run:

keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64

On Windows, run:

keytool -exportcert -alias androiddebugkey -keystore %HOMEPATH%\.android\debug.keystore | openssl sha1 -binary | openssl
base64

Debug key hashes source

For Release key hash:

On OS X, run: ( Replace what between <> with your values )

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

On Windows, use: ( Replace what between <> with your values )

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

Release key hashes source

查看更多
余欢
5楼-- · 2019-01-01 00:04

The generate hash key is wrong. You may get the hash key using two steps. One is through command prompt. Another one is through coding. Hash key through command prompt working on first time only. I don't know the reason. I have also got the same problem. So i tried it through programatically.

Follow this steps:

Paste the following code in oncreate().

try {
    PackageInfo info = getPackageManager().getPackageInfo(
                "com.example.packagename", 
                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) {

}

Modify "com.example.packagename" with your package name in the above coding without fail(You may found your package name in Android Manifest file).

Run your application. Go to the activity where you pasted the above code. In the logcat search for "KeyHash". You may found a key hash. Copy the key hash and go to Facebook application dashboard page. Go to settings and enter the details like in the bellow image.

enter image description here

Once you finished the above step. Relaunch the app again you may now log into the facebook. For more details about key hash check the link

If you add a wrong information in the settings page means it will give some error. so use the correct information there. And also if public(other than you) need to use your application means you need to enable the permission(change "yes" in the "Status & Review" next to setting).

查看更多
大哥的爱人
6楼-- · 2019-01-01 00:04

I experienced the same problem. I made a short research on the possible reasons for this strange behavior and I found the following:

  • During the first execution of a new Facebook app, it will allow connection/login even if you don't specify any key hashes.

  • For me, the tutorial which Facebook provided didn't generate the correct key hash, because it was giving the wrong configuration. When executing:

    keytool -exportcert -alias androiddebugkey -keystore %HOMEPATH%\.android\debug.keystore | openssl sha1 -binary | openssl
    base64
    

make sure you check all properties - the HOMEPATH, the existence of the keystore, etc. Maybe you also have to provide password.

  • What generated the proper configuration was the solution suggested by @Mahendran.

  • Also, if you see the error originally posted (http://i.stack.imgur.com/58q3v.png), most probably the key hash you see on the screen is your real one. If nothing else works, try inputting it in Facebook.

I got all those results with: Windows 7 64-bit edition, Android Studio 1.2.2, JDK 7.

查看更多
栀子花@的思念
7楼-- · 2019-01-01 00:06

According Facebook Login for Android, you must provide the Key Hash value. In order to obtain it, you will need the key used to sign your application.

keytool \
    -exportcert \
    -alias YourKeyAlias \
    -storepass YourStoreKeyPassword \
    -keystore PathToYourKeyStoreFile | openssl sha1 -binary | openssl base64
查看更多
登录 后发表回答