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

若如初见. 提交于 2019-12-22 13:07:32

问题


I was testing our app using test cases from Facebook's developers site.

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

Our app failed two following test cases:

  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

I have been trying to find the right method on the Facebook SDK for a way how to check this. Is there such method that would tell me that the user needs to log in again (in case of changing a password or removing the app from app settings on Facebook website)?


回答1:


I ended up making a "me" API call. If that fails on OAuthException I can be sure that something is wrong with the user login state.

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

Here is the 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);
        }
    }


来源:https://stackoverflow.com/questions/37022868/android-facebook-sdk-how-to-tell-if-user-changed-password-or-removed-app-fro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!