简体   繁体   中英

Android Facebook SDK login issue with parse sdk

I have a weird problem when logging using the LoginManager class. I am calling LoginManager.getInstance().logInWithReadPermissions(..) from a fragment to login with the required permissions.

I've debugged as far as I can and I found that the callback request code which returns to the onActivityResult of the parent activity of this fragment is wrong. The following piece of code is from the LoginManager class which registers the callback for login with some request code.

...
CallbackManagerImpl.registerStaticCallback(
              CallbackManagerImpl.RequestCodeOffset.Login.toRequestCode(),
              new CallbackManagerImpl.Callback() {
                 @Override
                 public boolean onActivityResult(int resultCode, Intent data) {
                   return LoginManager.this.onActivityResult(resultCode, data);
                 }
              }
      );
...

But when it calls the onActivityResult of the activity it's not the CallbackManagerImpl.RequestCodeOffset.Login.toRequestCode() but something else it's like double. For example when i tried to debug and check the values it was something like this:

CallbackManagerImpl.RequestCodeOffset.Login.toRequestCode() = 64206

requestCode received in Activity class = 129742

and now because of this when callbackManager tries to call the onActivityResult like

callbackManager.onActivityResult(requestCode, resultCode, data);

with this requestCode it doesn't find the logincallback from the map to go ahead and it stops right there. I don't know why it's happening as i am using ParseFacebookSDK and when i do login with ParseFacebookUtils.logInWithReadPermissionsInBackground(..) it works perfectly. Following is the gradle dependencies.

...
compile 'com.facebook.android:facebook-android-sdk:4.7.0'
compile('com.parse:parse-android:1.13.0') {
        exclude group: 'com.parse.bolts',
                module: 'bolts-tasks'
    }
compile 'com.parse:parsefacebookutils-v4-android:1.10.3@aar'
...

If you can help, would be greatly appreciated. Thanks.

UPDATE:

It worked for me when i tried facebook login from parent activity instead of fragment, it returned the correct request code in onActivityResult of activity this time and it simply worked. This has resolved my problem but still want to understand the problem with fragment here, so if you know please share your thoughts. Thanks.

Trying to call ParseFacebookUtils.logInWithReadPermissionsInBackground while your user is already logged is not correct, and probably that's why you don't get the callback.

The correct function to call if your user is already logged in, but not linked to Facebook, is ParseFacebookUtils.linkWithReadPermissionsInBackground , meaning that you want to link the user to facebook, not log in to your app (as he is already logged in). Take a look in the docs for that.

If you are calling LoginManager.getInstance().logInWithReadPermissions(..) from a fragment, then expect to receive the correct request code in onActivityResult of the fragment, rather than activity's onActivityResult .

See here . Fragments have their own 'onActivityResult` method. They need not depend on an activity for that.

Isn't it awesome that something that doesn't necessarily require any UI still provides this method by itself :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM