2

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.

Arpit Pruthi
  • 177
  • 10
  • Seeing your complete code including the merging of the service acocunt creation code with your code would help, especially the definition of `drive`. – ziganotschka Aug 10 '20 at 12:23
  • I hope now everything is clear @ziganotschka. – Arpit Pruthi Aug 10 '20 at 12:43
  • I believe that your error comes form the fact that you do not define `supportsAllDrives=true`. Please test it without a service account, from the [Try this API](https://developers.google.com/drive/api/v3/reference/files/create). – ziganotschka Aug 10 '20 at 12:56
  • Java client has setSupportsTeamDrives option for file.create function. I did'nt find this attribute supportsAllDrives. – Arpit Pruthi Aug 10 '20 at 13:06
  • 1
    Please see [here](https://googleapis.dev/java/google-api-services-drive/latest/com/google/api/services/drive/Drive.Files.Create.html#setSupportsAllDrives-java.lang.Boolean-). – ziganotschka Aug 10 '20 at 13:17
  • This option isn't available setSupportsAllDrives, only this is available setSupportsTeamDrives.. I am using compile 'com.google.api-client:google-api-client:1.23.0' compile 'com.google.oauth-client:google-oauth-client-jetty:1.23.0' compile 'com.google.apis:google-api-services-drive:v3-rev110-1.23.0' – Arpit Pruthi Aug 10 '20 at 13:53

1 Answers1

3

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
ziganotschka
  • 25,866
  • 2
  • 16
  • 33
Aerials
  • 4,231
  • 1
  • 16
  • 20
  • 1
    This option isn't available setSupportsAllDrives, only this is available setSupportsTeamDrives.. I am using compile 'com.google.api-client:google-api-client:1.23.0' compile 'com.google.oauth-client:google-oauth-client-jetty:1.23.0' compile 'com.google.apis:google-api-services-drive:v3-rev110-1.23.0' – Arpit Pruthi Aug 10 '20 at 13:52
  • 1
    And @Aerials @ziganotschka when I try to use for listing the contents `/*.setTeamDriveId("shared drive id") .setCorpora("drive") .setSupportsTeamDrives(true) .setIncludeTeamDriveItems(true)*/` I get the error that shared drive not found – Arpit Pruthi Aug 10 '20 at 13:55
  • @ArpitPruthi you want to use a *deprecated* method: `supportsTeamDrives`. If Gooogle deprecated it, well it will not serve you. Please read the description of the optional parameter "supportsTeamDrives" [here](https://developers.google.com/drive/api/v3/reference/files/create#parameters). That means you might want to upgrade the client to the latest version. – Aerials Aug 10 '20 at 14:01
  • I tried updating the java client and I got supportsAllDrives but still no luck it is not able to upload the data to shared drive .. I have created this shared drive manually and added the service account as content manager to that drive (if this is causing any issue). – Arpit Pruthi Aug 10 '20 at 14:33
  • 1
    To summarize: 1. You need to use `.setIncludeTeamDriveItems(true)` only, not `.setIncludeTeamDriveItems(true)`. 2. If you get the error that the drive is not found - it means either that the Id you provided is not correct or that you did not share the drive with the service account correctly. I suggest you one more time to test with the [Try this API](https://developers.google.com/drive/api/v3/reference/files/create) - this helps you to verify either the Drive id and other options are set correctly and narrow down the error source. – ziganotschka Aug 10 '20 at 15:30
  • Thank you so much @ziganotschka and Aerials it worked. I was giving wrong drive id, my bad. Thanks for your help – Arpit Pruthi Aug 11 '20 at 06:01