I tried to follow tutorial: https://developers.google.com/android/guides/http-auth.
Code:
token = GoogleAuthUtil.getToken(getApplicationContext(),
I found the answers here are passive solutions and not preventive.
According to my very short experience, UserRecoverableAuthException: NeedPermission will be thrown in the following 3 cases:
#1 Appropriate Scope is not requested when signing in to Google
Check that the scopes you are requesting are correct. Android should ask the user to allow permission after the authentication process via permission request dialog. This will prevent UserRecoverableAuthException when calling API.
GoogleSignInOptions o = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(new Scope(DriveScopes.DRIVE)) // request permission for Drive API
.requestScopes(new Scope(SheetsScopes.SPREADSHEETS)) // request permission for Sheets API
.requestEmail()
.build();
#2 User has denied permission
The user has pushed "DENY" button on permission request dialog.
#3 User has rejected permissions from your app
As for Android 8.1.x, there is a menu where you can reject permissions for individual apps. Not sure about other versions, though.
Settings > Google > Connected apps
UserRecoverableAuthException thrown by #2 and #3 is unavoidable because those are the result of user activity. But isn't that meaningless to show permission request dialog again with below code despite the user's rejection?
} catch (UserRecoverableAuthIOException e) {
startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
}