Recently, I have Android code which accesses to Google Drive. I\'m using Google APIs Client Library for Java instead of Google Play services client
I had the same problem, all permissions were granted and still I got this error.
My simple problem was, that the googleSignInAccount.getSignInAccount().getAccount()
was null
. This is the case, if you do NOT request the email permissions (GoogleSignInOptions.Builder.requestEmail
). Seems like this account can't be created without the user email.
So make sure to add this permission to the GoogleSignInOptions
...
I use google api client
// Dependency of Google Api client
implementation 'com.google.api-client:google-api-client:1.30.9'
implementation 'com.google.api-client:google-api-client-android:1.30.9'
implementation 'com.google.apis:google-api-services-drive:v3-rev20200413-1.30.9'
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(new Scope(Scopes.DRIVE_APPFOLDER))
//requestEmail required
.requestEmail()
.build();
GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
// in onActivityResult method
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
// androidAccount will be null if no email
android.accounts.Account androidAccount = account.getAccount();
You can see it from the source code of android.accounts.Account
private String zag;
@Field(
id = 4,
getter = "getEmail"
)
private String zah;
@Nullable
public Account getAccount() {
// getEmail required
return this.zah == null ? null : new Account(this.zah, "com.google");
}
It looks like the Drive API Client Library for Java calls into GoogleAuthUtil.getToken()
, which requires the GET_ACCOUNTS
permission. You need to have that permission in your manifest and request it at runtime, as appropriate.
UPDATE:
With Google Drive REST v3 API, GET_ACCOUNTS
permission is not required. Instead, Email permission is required. You can ask for it by calling GoogleSignInOptions.Builder.requestEmail
.
In my case (Google Drive REST API v3), ProGuard was the culprit, as the code was working fine in debug mode.
Just adding -keep class com.google.** { *;}
to ProGuard rules got rid of the issue.
PS: I had to call requestEmail()
but I did not ask for any GET_ACCOUNTS
permission.
This is because from Android 6.0 (API level 23)
you need to get the Contacts permission before you invoke google endpoints.
You can see complete details here - https://developer.android.com/training/permissions/requesting.html
This is true only for Dangerous permissions and permission groups (based on how much they are related to user privacy). More details - https://developer.android.com/guide/topics/security/permissions.html#perm-groups