Reputation: 4285
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 ?
Upvotes: 4
Views: 14531
Reputation: 126455
To do a correct validation check the accesstoken if it is null or expired:
AccessToken accessToken = AccessToken.getCurrentAccessToken();
boolean isLoggedIn = accessToken != null && !accessToken.isExpired();
if (isLoggedIn) {
//Post Facebook.
} else {
manager.logIn(shareAct, Arrays.asList("public_profile"));
}
Upvotes: 0
Reputation: 3948
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.
Upvotes: 3
Reputation: 9569
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"))
}
Upvotes: 3