Callback for Android refreshCurrentAccessTokenAsync in Facebook Android SDK 4.1+

前端 未结 2 1481
梦如初夏
梦如初夏 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.

    0 讨论(0)
  • 2020-12-22 06:36

    You cannot refresh the token if the user has revoked the token. You can only refresh it if the current access token is still valid.

    The method also calls Graph API directly, which is why there's no onActivityResult being called.

    If there is a successful refresh, then the access token will be updated and you can use the AccessTokenTracker to get notifications.

    Lastly, why are you calling this method directly? Normally just by using the Graph API the SDK will refresh the token automatically for you.

    0 讨论(0)
提交回复
热议问题