Uploading data to shared drive using service account in java

Viewed 1229

I want to upload the files to shared drive using java. I have created a service account and I have shared the shared-drive (name is DB-Drive) with the service account's address.

Global variables

private static HttpTransport httpTransport;
private static Drive drive;

Main function (initialize and listing of the files)

    httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    Credential credential = authorize();
    drive = new Drive.Builder(httpTransport, JacksonFactory.getDefaultInstance(), credential).setApplicationName(
                    "Quickstart").build();

    uploadFile();

    FileList result = drive.files().list().execute();
    List<File> files = result.getFiles();
    if (result == null || result.isEmpty()) {
        System.out.println("No files found.");
      } else {
        System.out.println("Files:");
        for (File file : files) {
             System.out.printf("%s %s \n", file.getName(),file.getId());
                }
            }
        } 

Authorization code

        GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("resources/credentials.json"))
                .createScoped(Collections.singleton(DriveScopes.DRIVE_FILE));

        return credential;

Uploading code

 String folderId = "***DB-Drive id****";
        File fileMetadata = new File();
        fileMetadata.setName("photo.jpg");
        fileMetadata.setParents(Collections.singletonList(folderId));
        java.io.File filePath = new java.io.File("/home/*****/Downloads/qck_1552369371.jpeg");
        FileContent mediaContent = new FileContent("image/jpeg", filePath);
        File file = drive.files().create(fileMetadata, mediaContent)
                .setFields("id, parents")
                .execute();
        System.out.println("File ID: " + file.getId());
        return file;

I am setting DB-Drive which is the teamdrive or the shareddrive as the parent of the uploaded file but still I am not able to view this in the shared drive folder.. Please help me with this.

I am adding the complete flow :-

1.) I create the service account using the steps mentioned at https://developers.google.com/identity/protocols/oauth2/service-account I selected the project and gave the description and everything.

2.) I did'nt gave domain level delegation to service account rather I shared the DB-drive(team drive) with service account address (which was mentioned in credentials.json file as client_email *****.gserviceaccount.com)

3.) Then I used the above code to upload the file asking service account to upload the file to shared drive i.e DB-Drive by specifying the DB-Drive's id as the parent id of the file.

4.) but still the file is getting uploaded to service account's drive it is not getting linked to shared drive.

5.) DB-Drive is the parent folder (the only folder).

Any leads would be a great help.

1 Answers

As @ziganotschka points out you need to set supportsAllDrives to true if creating a file in a shared drive. The Java client's setSupportsTeamDrives option is deprecated. Instead use setSupportsAllDrives to true.


When you have troubles uploading a file with a service ccount to a shared Drive, you should take the following steps for troubleshooting:

  • Isolate the problem
  • Ask yourself - is the problem related to the usage of the service account or something else?
  • For this, test the same request without a service account
  • If the service account turns out to be the culpit
    • Verify that you enabled domain-wide delegation correctly in the GCP console
    • Verify that you provided the correct scopes to the delegated service account in the Admin console
  • Verify that your code implementation is correct
  • If the problem is not service account related - verify either it is code syntax related or request related
  • For this, use the Try this API tool to test your request without coding
    • If the issue is coding related - debug your code
    • If the issue is request related, act according to the error the Try this API returns you
  • If you receive a 404 File not found error - it either means that
    • the file is located on a shared Drive (in this case you have to set supportsAllDrives to true as mentioned above)
    • or the fileId is not correct
    • Or the file is not accessible to the account you use for authentication
Related