2

I have integrated Facebook login into my application, but through testing I have found a bug and I am not sure whaat is the problem.

If my account for say is already logged into the device and I tap the button to login into Facebook, it will prompt the dialog "Continue As YOUR USERNAME" and it works fine.

But if I log out of my device on the phone and press the button to log-in using FB it will prompt me to login as which user, and after I login to a user I desire. After logging in it shows "This app has no key hashes configured etc..."

What could be the possible problem? Thanks in Advance! :D

This is my main activity class:

//Keys for Sharedpreferences
public static final String SHARED_PREF_NAME = "myloginapp";
public static final String EMAIL_SHARED_PREF = "email";
public static final String LOGGEDIN_SHARED_PREF = "loggedin";
private boolean loggedIn = false;

//FACEBOOK SDK
TextView text,userID,accessToken;
Button loginButton;
CallbackManager callbackManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());
    setContentView(R.layout.activity_main);

 //FACEBOOK SDK
    text = (TextView) findViewById(R.id.text);
    loginButton = (Button) findViewById(R.id.login_button);
   callbackManager = CallbackManager.Factory.create();

    LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {

            GraphRequest request = GraphRequest.newMeRequest(
                    AccessToken.getCurrentAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {

                        private  String uid,email,name;

                        @Override
                        public void onCompleted(
                                JSONObject object,
                                GraphResponse response) {
                            try {
                                uid = object.getString("id");
                                email = object.getString("email");
                                name = object.getString("name");

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            SharedPreferences sharedPreferences = MainActivity.this.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
                            SharedPreferences.Editor editor = sharedPreferences.edit();
                            editor.putBoolean(LOGGEDIN_SHARED_PREF, true);
                            editor.putString(EMAIL_SHARED_PREF, email);
                            editor.commit();

                            Intent intent = new Intent(MainActivity.this, userPage.class);
                            intent.putExtra("username", email);
                            startActivity(intent);
                            finish();
                        }
                    });
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name,email,gender");
            request.setParameters(parameters);
            request.executeAsync();
        }
        @Override
        public void onCancel() {
        }
        @Override
        public void onError(FacebookException error) {
        }
    });


    loginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("email"));
        }
    });

    }


@Override
protected void onResume() {
    super.onResume();

    SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);

    loggedIn = sharedPreferences.getBoolean(LOGGEDIN_SHARED_PREF, false);

    if(loggedIn){
        Intent intent = new Intent(MainActivity.this, userPage.class);
        startActivity(intent);
    }
}
Gionne Lapuz
  • 518
  • 1
  • 9
  • 23
  • 1
    did you add the Key hash in developer panel ? – rafsanahmad007 May 13 '17 at 17:48
  • @rafsanahmad007 yes sir that is what was missing i feel like an idiot haha. I actually followed a tutorial and they did not input a key has and the app worked. weird haha Thanks for the help sir! – Gionne Lapuz May 13 '17 at 18:19

1 Answers1

3

You have to configure your Android platform specific hash key in Facebook App settings.Facebook platform specific settings page

To get the hash key , use the following code snippet.Credit to Tarik.

 try {
        PackageInfo info =     getPackageManager().getPackageInfo("MY PACKAGE NAME",     PackageManager.GET_SIGNATURES);
        for (android.content.pm.Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            String sign=Base64.encodeToString(md.digest(), Base64.DEFAULT);
            Log.e("MY KEY HASH:", sign);

        }
} catch (NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
Community
  • 1
  • 1
JIthin
  • 1,413
  • 1
  • 13
  • 29
  • Yes you are right sir, I really feel stupid ahha. I followed this tutorial then he/she did not a key hash and it works. Weird haha. But thanks for the answer sir/ma'am you saved my life! :D – Gionne Lapuz May 13 '17 at 18:18