简体   繁体   中英

Android: Facebook always shows You have already authorized application dialog

I'm using Facebook SDK 4.1.2 and testing on Android 4.4.2 - Knowing that I have Facebook application uninstalled from my mobile for test purposes, which means that logging in and authorizing my application is via webview provided by FB SDK.

I get this message in a dialog everytime I clear the memory "You have already authorized {Application Name}"

How to avoid this message to appear repeatedly which will ruin user experience?

Knowing that and based on the code below, everytime I clear the mobile memory AccessToken.getCurrentAccessToken() returns null when application starts, which leads in my code to run the 2 lines below, which I believe is responsible for this message.

        List<String> PUBLIC_PROFILE_PERMISSION = Arrays.asList("public_profile");
        LoginManager.getInstance().logInWithReadPermissions(this, PUBLIC_PROFILE_PERMISSION); //Log in to FB

My logging in code flow, and please pitch in if it could be improved:

AccessToken mAccessToken;
CallbackManager callbackManager;
AccessTokenTracker accessTokenTracker;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FacebookSdk.sdkInitialize(this);
    callbackManager = CallbackManager.Factory.create();

    updateWithToken(AccessToken.getCurrentAccessToken());

    accessTokenTracker = new AccessTokenTracker()
    {
        @Override
        protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken newAccessToken)
        {
            updateWithToken(newAccessToken);
        }
    };
} 

// updateWithToken(...) function

private void updateWithToken(AccessToken currentAccessToken) {

    if (currentAccessToken != null)
    {
        mAccessToken = currentAccessToken;
        AccessToken.setCurrentAccessToken(currentAccessToken);
    }
    else
    {
        LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>()
        {
            @Override
            public void onSuccess(LoginResult loginResult)
            {
                // App code
                mAccessToken = loginResult.getAccessToken();
                AccessToken.setCurrentAccessToken(mAccessToken);
            }

            @Override
            public void onCancel()
            {
                // App code
            }

            @Override
            public void onError(FacebookException exception)
            {
                Log.d("", "");
            }
        });

        List<String> PUBLIC_PROFILE_PERMISSION = Arrays.asList("public_profile"); // public_profile
        LoginManager.getInstance().logInWithReadPermissions(this, PUBLIC_PROFILE_PERMISSION); //Log in to FB
    }
}

// onDestroy() function

@Override
public void onDestroy() {
    super.onDestroy();
    accessTokenTracker.stopTracking();
}

// onActivityResult(...) function

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
}

I used to get this message when in onSucesss() I logged in as follows:

Code

            GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(JSONObject object, GraphResponse response) {
                    try {
                        String s = object.getString("first_name") + " " + object.getString("last_name");
                        inputName.setText(s);
                        inputEmail.setText(object.getString("email"));
                    } catch (Exception e) {
                    }
                }
            }).executeAsync();
          }

But then I changed the last line as follows:

Code After Edit

            Bundle parameters = new Bundle();
            parameters.putString("fields", "id, first_name, last_name, email"); // parameters to get from facebook
            request.setParameters(parameters);
            request.executeAsync();

and that message disappeared.

EDIT 2

However, I just found out that after I logout from Facebook, using

                    LoginManager.getInstance().logOut();

and then I login again, the message came back. But as long as I did not logout programatically and instead I clicked on the Log Out button of Facebook (after I login the message on the button changes to "Log Out"), that message did not show up.

EDIT 3

Final solution to the problem is by deleting permission before logging out, as follows (replace the variable FBuserID with the actual userID you got from facebook using loginResult.getAccessToken().getUserId():

private void logOut(){
    GraphRequest delPermRequest = new GraphRequest(AccessToken.getCurrentAccessToken(),
            "/" + FBuserID + "/permissions/", null, HttpMethod.DELETE, new GraphRequest.Callback() {
        @Override
        public void onCompleted(GraphResponse graphResponse) {
            if (graphResponse != null){
                FacebookRequestError error = graphResponse.getError();
                if (error != null)
                    // error
                else
                    // SUCCESS
            }
        }
    });
    delPermRequest.executeAsync();
    LoginManager.getInstance().logOut();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM