0

Good afternoon!

I am having trouble accessing google Team Drive.

I need to create folders and upload files there from the local storage, and all this should be done by my application.

At the moment, I have learned to connect to my personal Google Drive and upload files there. My code for connecting to personal storage:

        Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential) //
                .setApplicationName(APPLICATION_NAME).build();


        // Print the names and IDs for up to 10 files.
        FileList result = service.files().list().setPageSize(10).setFields("nextPageToken, files(id, name)").execute();
        List<File> files = result.getFiles();
        if (files == null || files.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());
            }
        }

Tell me, how can I connect to Google Team Drive in java and upload files there? Thank you:)

Zeo
  • 53
  • 2
  • 10

1 Answers1

3

Connecting to the team drive with a service account in java

Assuming that you already fulfilled the prerequisites, which are

  • Creating a Google Service account
  • Downloading its credentials
  • Either share the team drive with the service account OR enable domain-wide delegation to impersonate a user who has access to the team drive

Your code should look something like this:

   private static final String APPLICATION_NAME = "YOUR APPLICATION";
   private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

   private static final List < String > SCOPES = Collections.singletonList("XXXINSERTHEREYOURSCOPEXXXX");

   public static void main(String...args) throws IOException, GeneralSecurityException {
    final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();

    File pk12 = new File("quickstartserv.p12");
    String serviceAccount = "EMAIL FO YOUR SERVICE ACCOUNT.iam.gserviceaccount.com";

    // Build service account credential.Builder necessary for the ability to refresh tokens

    GoogleCredential getCredentials = new GoogleCredential.Builder()
     .setTransport(HTTP_TRANSPORT)
     .setJsonFactory(JSON_FACTORY)
     .setServiceAccountId(serviceAccount)
     .setServiceAccountPrivateKeyFromP12File(pk12)
     .setServiceAccountScopes(SCOPES)
     .setServiceAccountUser("xxx") //IF YOU WANT TO IMPERSONATE A USER
     .build();

    // Build a new authorized API client service.

    Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials)
     .setApplicationName(APPLICATION_NAME)
     .build();

     FileList result = service.files().list().setPageSize(10).setQ('"ID OF THE SHARED DRIVE" in parents').setIncludeTeamDriveItems(true).setSupportsTeamDrives(true).setFields("nextPageToken, files(id, name)").execute();
     ...
   }

Mind that setIncludeTeamDriveItems(true) and setSupportsTeamDrives(true) are necessary to retrieve files from the shared drive.

ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • Thanks @ziganotschka for such nice explanation but when I upload the data to teamdrive I am not able to see the contents in drive.. I have shared the team drive with service account and setting the parent drive as the id of the shared drive but still no luck .. Can you help with uploading the data to team drive? – Arpit Pruthi Aug 10 '20 at 10:48
  • Does your code run without error? Are you sure the service account uploads the files to the correct shred Drive (check the Id)? Do you have permissions to view the contents of the drive in question? – ziganotschka Aug 10 '20 at 10:56
  • This the code that I am using to upload the file https://stackoverflow.com/questions/63338864/uploading-data-to-shared-drive-using-service-account-in-java. Code runs without any error but when I view the files from drive UI I am not able to see any file there. – Arpit Pruthi Aug 10 '20 at 11:01
  • When I list the files using java the photo.jpeg is listed independent of the parent drive. I mean like it is not created inside the parent drive but it present along with parent drive only. – Arpit Pruthi Aug 10 '20 at 11:07
  • Does it work if you add `.setSupportsTeamDrives(true)` to the `file resource`? – ziganotschka Aug 10 '20 at 11:16
  • No it does'nt work . If I check parent of every file it is null. – Arpit Pruthi Aug 10 '20 at 11:20
  • Can you upload a file onto the dirve in question without a service account? If yes, please provide on your post the full code including the way how you build your service account. If no - can you create a file wihtout a service account on your own drive? – ziganotschka Aug 10 '20 at 11:23
  • Use case for my application is that every one in the team should able to upload the files to a shared drive so I require a service account I don't want to access the user accounts or user data. I will update my post with the detailed steps. – Arpit Pruthi Aug 10 '20 at 11:27
  • I have updated the post with complete flow, please let me know if you need any other information. @ziganotschka – Arpit Pruthi Aug 10 '20 at 12:12
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/219542/discussion-between-arpit-pruthi-and-ziganotschka). – Arpit Pruthi Aug 10 '20 at 12:16