问题
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 ?
回答1:
Just do something like:
@Override
public void onError(FacebookException error) {
Timber.e(error, "Error during facebook login");
AccessToken.setCurrentAccessToken(null);
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email"))
}
回答2:
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.
来源:https://stackoverflow.com/questions/29822082/error-validating-access-token-the-user-has-not-authorized-application-facebook