【问题标题】:How to access Team Drive using service account with Google Drive API v3 Java如何通过 Google Drive API v3 Java 使用服务帐户访问 Team Drive
【发布时间】:2020-07-14 14:24:39
【问题描述】:

下午好!

我无法访问 Google Team Drive。

我需要创建文件夹并从本地存储上传文件,所有这些都应该由我的应用程序完成。

目前,我已经学会了连接到我的个人 Google 云端硬盘并在那里上传文件。我的个人存储连接代码:

        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());
            }
        }

告诉我,如何在 Java 中连接到 Google Team Drive 并在那里上传文件? 谢谢:)

【问题讨论】:

    标签: java google-drive-api google-drive-shared-drive


    【解决方案1】:

    使用 java 中的服务帐户连接到团队驱动器

    假设您已经满足了先决条件,即

    • 创建 Google 服务帐户
    • 下载其凭据
    • 要么与服务帐户共享团队驱动器,要么启用域范围的委派以模拟有权访问团队驱动器的用户

    您的代码应如下所示:

       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();
         ...
       }
    
    

    请注意,setIncludeTeamDriveItems(true)setSupportsTeamDrives(true) 是从共享驱动器中检索文件所必需的。

    【讨论】:

    • 感谢 @ziganotschka 提供如此好的解释,但是当我将数据上传到 teamdrive 时,我无法看到驱动器中的内容。我已将团队驱动器与服务帐户共享并将父驱动器设置为共享驱动器的 ID,但仍然没有运气.. 你能帮忙将数据上传到团队驱动器吗?
    • 你的代码运行没有错误吗?您确定服务帐户将文件上传到正确的粉碎驱动器(检查 ID)吗?您是否有权查看相关驱动器的内容?
    • 这是我用来上传文件stackoverflow.com/questions/63338864/…的代码。代码运行时没有任何错误,但是当我从驱动器 UI 查看文件时,我看不到那里的任何文件。
    • 当我使用 java 列出文件时 photo.jpeg 独立于父驱动器列出。我的意思是它不是在父驱动器内创建的,而是仅与父驱动器一起出现。
    • .setSupportsTeamDrives(true)加到file resource上能用吗?
    猜你喜欢
    • 2017-08-31
    • 1970-01-01
    • 2019-03-07
    • 2021-12-21
    • 2020-10-25
    • 2017-06-29
    • 1970-01-01
    • 2017-08-29
    • 1970-01-01
    相关资源
    最近更新 更多