7

I am building an app which requires facebook login and authentication.

I am following

https://developers.facebook.com/docs/android/getting-started#create-app

I got this error: when I hit the facebook login button:

enter image description here

Multiple posts say that this code should resolve the error

Key hash doesn't match while facebook login in android

:

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

        }

however the toByteArray() method cannot be resolved!

enter image description here

How can I resolve this error and achieve the simple facebook login for my app?

Community
  • 1
  • 1
Rohit Tigga
  • 2,373
  • 9
  • 43
  • 81

2 Answers2

43

You must import a android.content.pm.Signature and not a java.security.Signature.

Nicolas Albert
  • 2,586
  • 1
  • 16
  • 16
6
from Logcat you can get hash key of facebook please do copy from logcat which having Logcat tag "KeyHash" and put it in your project on developer.facebook site 

enter image description here

import android.content.pm.Signature;

      try {
        PackageInfo info = getPackageManager().getPackageInfo(
                **"do not forgot to 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) {

    }



OR 

1. for Android default keystore  : add this to in your terminal 
 
keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64

2. for signed keystore 

keytool -exportcert -alias aliasname -keystore keystorename | openssl sha1 -binary | openssl base64
Vaishali Sutariya
  • 5,093
  • 30
  • 32