Zeo
Zeo

Reputation: 53

How to access Team Drive using service account with Google Drive API v3 Java

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:)

Upvotes: 0

Views: 1307

Answers (1)

ziganotschka
ziganotschka

Reputation: 26796

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.

Upvotes: 3

Related Questions