简体   繁体   English

如何使用带有 Google Drive API v3 Java 的服务帐户访问 Team Drive

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

Good afternoon!下午好!

I am having trouble accessing google Team Drive.我在访问 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.目前,我已经学会了连接到我的个人 Google Drive 并在那里上传文件。 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?告诉我,如何在 Java 中连接到 Google Team Drive 并在那里上传文件? Thank you:)谢谢:)

Connecting to the team drive with a service account in java使用 java 中的服务帐户连接到团队驱动器

Assuming that you already fulfilled the prerequisites, which are假设您已经满足先决条件,即

  • Creating a Google Service account创建 Google 服务帐户
  • 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.请注意setIncludeTeamDriveItems(true)setSupportsTeamDrives(true)是从共享驱动器检索文件所必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Drive v3 Java API获取服务帐户的配额 - Google drive v3 java API to get quota for service account 使用 Android java 应用程序中的服务帐户连接到 google Drive API v3 - Connecting to google Drive API v3 with a service account in Android java app 如何使用 Google Drive Java API v3 复制 Google Drive 上的文件? - How do I copy a file on a Google Drive using v3 of the Google Drive Java API? 尝试使用适用于Java的Drive API v3向TeamDrive中的文件添加属性 - Trying to add properties to files in TeamDrive using Drive API v3 for Java and a service account 使用 Java 和 Google Drive API V3 将文件上传到共享的 Google Drive 位置? - Upload a file to shared google drive location using Java with Google Drive API V3? 如何使用Java搜索Google Drive V3中的文件夹? - How to search the folder in the google drive V3 using java? Google Drive Java API V2与V3 - Google Drive Java API V2 vs V3 如何使用Java中的Google Drive API库强制从Google Team Drive下载文件 - How to force download of file from Google Team Drive using Google Drive API libraries in Java 使用 Google 列出 Google 云端硬盘文件夹中的文件 API v3 JAVA - Listing files in Google Drive Folder with Google API v3 JAVA 如何解决不足的文件权限创建Google服务帐户团队驱动器的错误? - How to resolve Google Service Account Team Drive creation error of insufficientFilePermissions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM