android connect facebook invalid keyhash

2020-06-29 08:48发布

问题:

i'm working android facebook sdk. i have problem when divice has installed facebook application(invalid key hash) i recived keyhash in this code

public class SpleshScreen extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_splesh_screen);

    try {
        PackageInfo info = getPackageManager().getPackageInfo(
                "mypackage", 
                PackageManager.GET_SIGNATURES);
        for (android.content.pm.Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.wtf("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
            }
    } catch (NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }

    Thread background = new Thread() {

        public void run() {

            try {
                // Thread will sleep for 2 seconds
                sleep(2 * 1000);


                //
                // Log.e("asdasdasdasd",my_json );

                // After 2 seconds redirect to another intent
                Intent in = new Intent(getApplicationContext(),
                        MainmoviesActivity.class);
                startActivity(in);

                // Remove activity
                finish();

            } catch (Exception e) {

            }
        }
    };

    // start thread
    background.start();
}

}

and i added this keyhash in my facebook app . i have problem only when divice has facebook application(when i run my app with USB ) what am i doing wrong? if anyone knows solution please help me

回答1:

Its because the generate hash key is wrong.

To solve 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) {

}

see this thread

https://stackoverflow.com/a/23863110/2176734

this will surely solve your problem.



回答2:

1.go to developer.facebook.com

2.select your application

3.Click settings

4.click Add Platform

5.add all mandatory fields

6.add your key hashes there

7.Click save changes

now check