Callback for Android refreshCurrentAccessTokenAsync in Facebook Android SDK 4.1+

前端 未结 2 1496
梦如初夏
梦如初夏 2020-12-22 06:05

In Android, I\'m calling refreshCurrentAccessTokenAsync() but there doesn\'t appear to be a callback. I\'ve registered callbackManager before invoking re

2条回答
  •  忘掉有多难
    2020-12-22 06:28

    Sadly this is because of the poor documentation of the SDK by FaceBook.

    You have to set a tracker for AccessToken, similar to what you'd do with Profile.

    My code for that is as below:

    private AccessTokenTracker mAccessTokenTracker;
    
    private void loginToMyFbApp() {
        FacebookSdk.sdkInitialize(this);
        if (AccessToken.getCurrentAccessToken() != null) {
            mAccessTokenTracker = new AccessTokenTracker() {
                @Override
                protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
                    mAccessTokenTracker.stopTracking();
                    if(currentAccessToken == null) {
                        //(the user has revoked your permissions -
                        //by going to his settings and deleted your app)
                        //do the simple login to FaceBook
                    }
                    else {
                        //you've got the new access token now.
                        //AccessToken.getToken() could be same for both
                        //parameters but you should only use "currentAccessToken"
                    }
                }
            };
            AccessToken.refreshCurrentAccessTokenAsync();
        }
        else {
            //do the simple login to FaceBook
        }
    }
    

    Edit

    Removed startTracking() call as rightly pointed out by Astrount in the comment below.

提交回复
热议问题