cgr
cgr

Reputation: 4636

Can't get Access Token for Facebook SDK 4.8.1 for Android (OR) FacebookCallback is not called (OR) requestCode is different in Facebook integration

I followed Facebook login for Android to integrate FB to my Android app. It is done. I see the permission screen where user has to accepet to use his email ID. Issue is that none of the callbacks of FacebookCallback are getting called.

I followed the same procedure mentioned in the above tutorial.

Fragment code:

    public class MyFragment extends Fragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            FacebookSdk.sdkInitialize(this.getActivity().getApplicationContext());
           super.onCreate(savedInstanceState);
        }  

        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
                mButtonFbLogin.setReadPermissions(Arrays.asList("email"));
            mButtonFbLogin.setFragment(this);

            mCallbackManager = CallbackManager.Factory.create();

            mButtonFbLogin.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    Log.d(TAG, "FB Login Successful. Token : " +loginResult.getAccessToken());
                }

                @Override
                public void onCancel() {
                    Log.d(TAG, "FB Login cancelled..");
                }

                @Override
                public void onError(FacebookException e) {
                    Log.d(TAG, "FB Login failed. Exception : " +e.toString());
                }
            });
       }   

       @Override
       public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            Log.d(TAG, "onActivityResult of Fragment..");
            mCallbackManager.onActivityResult(requestCode, resultCode, data);

    }

}

Through logs, I see that onActivityResult of my fragment is called and so onActivityResult() of callbackManger too. But none of the callbacks of Facebook Callback are called.

Any clue ?

AccessToken.getCurrentAccessToken() is a way which can be used in onCreate(). But with this I can not exactly know when first time the token is created.

And AccessTokenTracker is actually to track the changes in tokens but not to know when the login session is success, fail or cancelled.

UPDATE:

I just tried with AccessTokenTracker but onCurrentAccessTokenChanged() was not called after I pressed OK on permission screen and control is back to my fragment. This is the code:

@Override public void onCreate(Bundle savedInstanceState) { FacebookSdk.sdkInitialize(this.getActivity().getApplicationContext());

mAccessTokenTracker = new AccessTokenTracker() {
    @Override
    protected void onCurrentAccessTokenChanged(
            AccessToken oldAccessToken,
            AccessToken currentAccessToken) {
        Log.d(TAG, "onCurrentAccessTokenChanged : old : " +oldAccessToken);
        Log.d(TAG, "onCurrentAccessTokenChanged : new : " +currentAccessToken);
    }
};
mAccessTokenTracker.startTracking();

super.onCreate(savedInstanceState);

}

Upvotes: 3

Views: 820

Answers (1)

cgr
cgr

Reputation: 4636

I had MainActivity which holds MainFragment which spawns CHildFragment. ChildFragment is where the LoginButton was added.

MainActivity -> MainFragment -> ChildFragment (LoginButton).

I had onActivityResult() in MainActivity and ChildFragment too. In onActivityResult() of MainActivity, I explicitly called childFragment.onActivityResult() where I called CallbackManager.onActivityResult(). I did setFragment(this) in ChildFragment. All fine but none of the callbacks in FacebookCallback were getting called.

I tried multiple solutions and debugged Facebook code too and found that the requestCode they use as index to store callback we sent. This code was different while storing the callback and while retrieving it. So nothing happened.

Part of debuggin, I moved the complete code to MainFragment and it worked. :-)

So, solution was to override onActivityResult() in MainFragment and called onActivityResult() of ChildFragment from there. IT WORKED.

So, the conclusion is that if you put LoginButton in child/nested fragment, Facebook sdk will call the onActivityResult of parent fragment but not the onActivityResult() of child fragment or Activity.

Upvotes: 2

Related Questions