Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
}
like image 910
Ashraf Alshahawy Avatar asked Nov 09 '22 13:11

Ashraf Alshahawy


1 Answers

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();
}
like image 188
Zvi Avatar answered Nov 14 '22 22:11

Zvi