Skip to content Skip to sidebar Skip to footer

Callback For Android Refreshcurrentaccesstokenasync In Facebook Android Sdk 4.1+

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

Solution 1:

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:

privateAccessTokenTracker mAccessTokenTracker;

privatevoidloginToMyFbApp() {
    FacebookSdk.sdkInitialize(this);
    if (AccessToken.getCurrentAccessToken() != null) {
        mAccessTokenTracker = newAccessTokenTracker() {
            @OverrideprotectedvoidonCurrentAccessTokenChanged(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.

Solution 2:

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.

Post a Comment for "Callback For Android Refreshcurrentaccesstokenasync In Facebook Android Sdk 4.1+"