【问题标题】:Callback for Android refreshCurrentAccessTokenAsync in Facebook Android SDK 4.1+Facebook Android SDK 4.1+ 中的 Android refreshCurrentAccessTokenAsync 回调
【发布时间】:2015-08-12 09:45:10
【问题描述】:

在 Android 中,我正在调用 refreshCurrentAccessTokenAsync(),但似乎没有回调。我在调用 refreshCurrentAccessTokenAsync() 之前注册了 callbackManager 并且它永远不会被调用,onActivityResult 也不会。

Doc 也没有说什么: https://developers.facebook.com/docs/reference/android/current/class/AccessToken/

在 iOS 中,有一个完成处理程序。

+ (void)refreshCurrentAccessToken:(FBSDKGraphRequestHandler)completionHandler;

如果用户撤销其令牌或删除权限,最佳做法是什么?

我假设在 IOS 上我会检查 GraphRequest 完成处理程序,但看不到在没有回调的 Android 上推荐的方式是什么。

谢谢!

【问题讨论】:

  • 你检查我的答案了吗?如果您像我一样主要使用 FB 登录您的服务器(使用 FB id),这是正确的方法。

标签: android ios facebook


【解决方案1】:

很遗憾,这是因为 FaceBook 的 SDK 文档很差。

您必须为AccessToken 设置跟踪器,类似于您对Profile 所做的操作。

我的代码如下:

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
    }
}

编辑

删除了startTracking() 调用,正如Astrountthe comment below 中正确指出的那样。

【讨论】:

  • 不错的实现,但你不需要 mAccessTokenTracker.startTracking();作为 new AccessTokenTracker() 内部调用 startTracking();
  • @Astrount 谢谢。你说得对;我现在已经删除了那条线。 :)
【解决方案2】:

如果用户撤销了令牌,您将无法刷新令牌。只有在当前访问令牌仍然有效时才能刷新它。

该方法也直接调用Graph API,所以没有调用onActivityResult。

如果刷新成功,则访问令牌将被更新,您可以使用 AccessTokenTracker 获取通知。

最后,你为什么要直接调用这个方法?通常只需使用 Graph API,SDK 就会自动为您刷新令牌。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-23
    相关资源
    最近更新 更多