Error validating access token: The user has not authorized application. Facebook SDK 4

Danylo Volokh picture Danylo Volokh · Apr 23, 2015 · Viewed 10.8k times · Source

Here is the case :

If I previously granted read permissions to my application via

LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email"));

Then, when I need to share I request publish permissions via :

LoginManager.getInstance().logInWithPublishPermissions(getActivity(), Arrays.asList("publish_actions"));

Everything works fine until I removed permissions for my application in Web.

If Application was killed or stopped and I need to share I will check if I still have the permissions:

if(userAuthorizedMyApp()){
    LoginManager.getInstance().logInWithPublishPermissions(this, Arrays.asList("publish_actions"));
} else {
    LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email"));
}

Method userAuthorizedMyApp() returns false (accessToken null):

private boolean userAuthorizedMyApp() {
    boolean authorized;
    AccessToken accessToken = AccessToken.getCurrentAccessToken();
    if(DEBUG) Log.v(TAG, "accessToken [" + accessToken +"]");

    if(accessToken != null){
        Set<String> currentPermissions = accessToken.getPermissions();
        authorized = currentPermissions.contains("public_profile");
    } else {
        authorized = false;
    }
    if(DEBUG) Log.v(TAG, "userAuthorizedMyApp[" + authorized +"]");

    return authorized;
}

And I try to do usual login, as if it were first time :

LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email"));

And I receive :

public void onError(FacebookException error)

onError, error {HttpStatus: -1, errorCode: 190, errorType: null, errorMessage: Error validating access token: The user has not authorized application 123456789123456.}

Any ideas what I am doing wrong ?

Answer

bentobox picture bentobox · Jul 6, 2015

Calling AccessToken.refreshCurrentAccessTokenAsync() worked for me. I get the error in the implementation of FacebookCallback.onError(), where I call AccessToken.refreshCurrentAccessTokenAsync() which clears the access token in Facebook SDK.

Because this is an async call, I cannot call loginManager.logInWithReadPermissions() immediately; There is no callback, so I display an AlertDialog to the user and when the user clicks "OK", at that time I call loginManager.logInWithReadPermissions().

So user experience is not too bad, and it is acceptable for an edge case.