Google Drive API - the name must not be empty: null (But I had passed valid account name to GoogleAccountCredential)

前端 未结 5 1180
自闭症患者
自闭症患者 2020-12-03 08:12

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

5条回答
  •  抹茶落季
    2020-12-03 08:57

    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 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");
        }
    
    

提交回复
热议问题