Facebook login with Parse always returns false in

2020-07-27 16:03发布

问题:

I have an issue with Facebook login in Android using Parse. Everything is working fine but I always get User logged in through Facebook.

I found a guy with the same problem here Facebook Login not working properly (Parse) but his answer doesnt work for me.

        List<String> permissions = Arrays.asList("email");
        ParseFacebookUtils.logIn(permissions, getActivity(), new LogInCallback() {
            @Override
            public void done(ParseUser user, ParseException err) {

                if (user == null) {
                    Log.d("WelcomeMainFragment",
                            "Uh oh. The user cancelled the Facebook login.");

                } else if (user.isNew()) {
                    Log.d("WelcomeMainFragment",
                            "User signed up and logged in through Facebook!");


                } else {
                    Log.d("WelcomeMainFragment",
                            "User logged in through Facebook!");
                }
            }
        });

user.isNew() always returns false! even with new facebooks accounts in my app. I need to have working isNew() method to be able to create parse users for first time in that block of code.

Thanks a lot.

回答1:

After some research I found what the problem was. Basically I had two problems:

  1. I wasn't getting the key hash properly.

keytool -exportcert -alias YOUR_RELEASE_KEY_ALIAS -keystore YOUR_RELEASE_KEY_PATH | openssl sha1 -binary | openssl base64

Doing above I was getting the wrong key hash. So I encourage you to get it with this piece of code:

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

You have to execute it in debug and production to get both keys.

  1. The most important problem was that I had to allow Facebook authentication on Parse and include the Facebook app id and app secret. You can do that going to Settings > Authentication > Facebook.

I hope that can help someone.