Android - Facebook SDK - 如何判断用户是否更改了密码或从 Facebook 上的应用设置中删除了应用

Android - Facebook SDK - how to tell if user changed password or removed app from app settings on Facebook

我正在使用来自 Facebook 开发者网站的测试用例来测试我们的应用程序。

https://developers.facebook.com/docs/facebook-login/testing-your-login-flow

我们的应用未能通过以下两个测试用例:

  1. Someone removes your app from Facebook via app settings and revisits your app. Your app should detect this and prompt the person to log back in.

    • Go to your app and tap on the "Log in with Facebook” button Tap OK to accept the read permissions (and OK again to accept write permissions where applicable)

    • Go to app settings on Facebook and remove your app

    • Repeat steps 1-2 and verify that Facebook Login works

  2. Someone changes the Facebook password after logging in with Facebook to your app In this case, your token will be invalid and you should notify users that their Facebook session has expired and ask them to log in again.

    • Change your Facebook password and select “Log me out of other devices”

    • Go to your app and tap on the "Log in with Facebook” button

    • Tap OK to accept the read permissions (and OK again to accept write permissions where applicable)

    • Go to app settings on Facebook and verify that the granted permissions are there

我一直试图在 Facebook SDK 上找到正确的方法来检查这个。是否有这样的方法会告诉我用户需要重新登录(以防更改密码或从 Facebook 网站上的应用程序设置中删除应用程序)?

我最后打了一个 "me" API 电话。如果在 OAuthException 上失败,我可以确定用户登录状态有问题。

    Action<JSONObject, GraphResponse> facebookMeCallbackAction =
        delegate (JSONObject jsonObject, GraphResponse graphResponse)
        {
            try
            {
                if (graphResponse.Error == null)
                {
                    // Do whatever we need to do in regards to Facebook
                }
                else
                {
                    if (graphResponse.Error.ErrorType.Equals(FacebookMeCallback.OAuthExceptionExceptionType, StringComparison.InvariantCultureIgnoreCase))
                    {
                        // Something wrong is with the login state, ask for reconnection to Facebook, etc.
                    }
                    else
                    {
                        // Something else went wrong
                    }
                }
            }
            catch (Exception ex)
            {
                Util.LogException(ex);
            }
        };

    using (var facebookCallback = new FacebookMeCallback(facebookMeCallbackAction))
    {
        var newMeRequest = GraphRequest.NewMeRequest(AccessToken.CurrentAccessToken, facebookCallback);
        GraphRequest.ExecuteAndWait(newMeRequest);
    }

这是 FacebookMeCallback class:

public class FacebookMeCallback : Java.Lang.Object, GraphRequest.IGraphJSONObjectCallback
    {
        public const string OAuthExceptionExceptionType = "OAuthException";

        private readonly Action<JSONObject, GraphResponse> onCompletedFacebookCallbackAction;

        public FacebookMeCallback(Action<JSONObject, GraphResponse> onCompletedFacebookCallbackAction)
        {
            this.onCompletedFacebookCallbackAction = onCompletedFacebookCallbackAction;
        }

        public void OnCompleted(JSONObject jsonObject, GraphResponse graphResponse)
        {
            if (onCompletedFacebookCallbackAction != null)
                onCompletedFacebookCallbackAction(jsonObject, graphResponse);
        }
    }