Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
914 views
in Technique[技术] by (71.8m points)

ios - 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() and it never gets called, and neither does onActivityResult.

Doc doesn't say anything either: https://developers.facebook.com/docs/reference/android/current/class/AccessToken/

In iOS, there is a completion handler.

+ (void)refreshCurrentAccessToken:(FBSDKGraphRequestHandler)completionHandler;

What is the best practice to use in the event a user revoked their token or removed permissions?

I assume on IOS I would check in the GraphRequest completion handler, but don't see what's the recommended way on Android with no callback.

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...