I am trying drive api on android with the sample code from the sdk but getting this error. The permissions to access the app folder is granted. I have also tried deleting the hidden data and disconnecting the app from drive and reconnecting back, but of no use. The error remains same. Below is my code:
private void createFileInAppFolder() {
final Task<DriveFolder> appFolderTask = getDriveResourceClient().getAppFolder();
final Task<DriveContents> createContentsTask = getDriveResourceClient().createContents();
Tasks.whenAll(appFolderTask, createContentsTask)
.continueWithTask(new Continuation<Void, Task<DriveFile>>() {
@Override
public Task<DriveFile> then(@NonNull Task<Void> task) throws Exception {
DriveFolder parent = appFolderTask.getResult();
DriveContents contents = createContentsTask.getResult();
OutputStream outputStream = contents.getOutputStream();
try {
Writer writer = new OutputStreamWriter(outputStream);
writer.write("Hello World!");
}catch (Exception e){
}
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle("New file")
.setMimeType("text/plain")
.setStarred(true)
.build();
return getDriveResourceClient().createFile(parent, changeSet, contents);
}
})
.addOnSuccessListener(this,
new OnSuccessListener<DriveFile>() {
@Override
public void onSuccess(DriveFile driveFile) {
Log.e("BACKUP","Success"+driveFile.getDriveId().encodeToString());
finish();
}
})
.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e("BACKUP", "Unable to create file", e);
}
});
}
protected void signIn() {
Set<Scope> requiredScopes = new HashSet<>(2);
requiredScopes.add(Drive.SCOPE_FILE);
requiredScopes.add(Drive.SCOPE_APPFOLDER);
GoogleSignInAccount signInAccount = GoogleSignIn.getLastSignedInAccount(this);
if (signInAccount != null && signInAccount.getGrantedScopes().containsAll(requiredScopes)) {
initializeDriveClient(signInAccount);
} else {
GoogleSignInOptions signInOptions =
new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(Drive.SCOPE_FILE)
.requestScopes(Drive.SCOPE_APPFOLDER)
.build();
GoogleSignInClient googleSignInClient = GoogleSignIn.getClient(this, signInOptions);
startActivityForResult(googleSignInClient.getSignInIntent(), REQUEST_CODE_SIGN_IN);
}
}
Error log:
E/BACKUP: Unable to create file
com.google.android.gms.common.api.ApiException: 1502: Invalid parent folder.
at com.google.android.gms.internal.zzbsv.onError(Unknown Source)
at com.google.android.gms.internal.zzbql.onTransact(Unknown Source)
at android.os.Binder.execTransact(Binder.java:453)
Please help..Thanks in advance