0

I have a problem with facebook login. I used below code to login to facebook but it gives me Invalid Key hash error. I added the hash key to facebook developer as well but I don't know what is the problem that can not login into facebook? Previously it was login but right now it gives hashkey error

public void loginToFacebook() {

    mPrefs = getPreferences(MODE_PRIVATE);
     access_token = mPrefs.getString("access_token", null);
     expires = mPrefs.getLong("access_expires", 0);

    if (access_token != null) {
      facebook.setAccessToken(access_token);

   //   btnFbLogin.setVisibility(View.INVISIBLE);

      // Making get profile button visible


      // Making post to wall visible


      // Making show access tokens button visible


      Log.d("FB Sessions", "" + facebook.isSessionValid());
    }

    if (expires != 0) {
      facebook.setAccessExpires(expires);
    }

    if (!facebook.isSessionValid()) {
      facebook.authorize(this,
          new String[] { "email"},
          new DialogListener() {

            @Override
            public void onCancel() {
              // Function to handle cancel event
            }

            @Override
            public void onComplete(Bundle values) {
              // Function to handle complete event
              // Edit Preferences and update facebook acess_token

                SharedPreferences.Editor editor = mPrefs.edit();
              editor.putString("access_token",
                  facebook.getAccessToken());
              editor.putLong("access_expires",
                  facebook.getAccessExpires());
              editor.commit();

              // Making Login button invisible
             // btnFbLogin.setVisibility(View.INVISIBLE);

              // Making logout Button visible
              getProfileInformation();

              // Making post to wall visible

              // Making show access tokens button visible

            }

            @Override
            public void onError(DialogError error) {
              // Function to handle error

            }

            @Override
            public void onFacebookError(FacebookError fberror) {
              // Function to handle Facebook errors

            }

          });
    }
  }

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    facebook.authorizeCallback(requestCode, resultCode, data);
  }
user3669034
  • 99
  • 2
  • 4
  • 11

3 Answers3

2

If it was working before I would recommend to wait for few hours as many android developres including me facing this issue since today morning.

https://developers.facebook.com/bugs/1755756591335035/?comment_id=2027873617436930

According to official member comment, it should get resolved in next few hours.

Bhavesh Patadiya
  • 25,740
  • 15
  • 81
  • 107
0

Genrate your hash key by below code put in Main Activty after setContentView()

try {
        PackageInfo info = getPackageManager().getPackageInfo(
                "yourpackegename", //set your package name
                PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.e("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
    } catch (PackageManager.NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }

And Copy hashCode From Log and past In your facebook dashboard

Dixit Panchal
  • 3,406
  • 1
  • 11
  • 14
0

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. So try with the hashkey which is generated programatically.

In OnCreate()

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

}

After running code you will get hashkey in logcat.Put that hashkey in setting page of Facebook developer account page.

Ajit Sharma
  • 411
  • 3
  • 8
  • I doesn't work again, I used this code more than 10 times, The error is something else. it was working till today but from today starting to give this error. – user3669034 Jun 02 '16 at 06:29
  • There is no log out error i just have problem with connecting to facebook it says invalid hash I delete the app and create it again and create hash key using openssl and the check with above code and add it to facebook developer but still couldn't connect – user3669034 Jun 02 '16 at 06:39