为什么在 google-api-services-drive 中使用共享驱动器的 fileId 时 "File not found"?

Why "File not found" when using fileId of shared drive in google-api-services-drive?

下面的代码试图授予一个组 fileOrganizer 共享驱动器的权限,我之前使用类似的代码和模拟同一用户的相同服务帐户创建了共享驱动器。 (我用假的编辑了实际的 DRIVE_ID

public class DriveQuickstart {
    public static void main(String... args) throws IOException, GeneralSecurityException {
        final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
        final JsonFactory JSON_FACTORY        = JacksonFactory.getDefaultInstance();
        final List<String> SCOPES             = Collections.singletonList(DriveScopes.DRIVE);
        final String JSON_PATH                = "my.json";
        final String USER                     = "user@domain.com";
        final String GROUP                    = "group@domain.com";
        final String DRIVE_ID                 = "bbb";

        GoogleCredential credential = GoogleCredential
            .fromStream(new FileInputStream(JSON_PATH))
            .createScoped(SCOPES)
            .createDelegated(USER);

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

        DriveList result = service.drives().list().execute();
        System.out.println("drive list " + result);
        System.out.println("about to grant permission on id " + DRIVE_ID);

        service
            .permissions()
            .create(DRIVE_ID,
                    new Permission()
                    .setType("group")
                    .setRole("fileOrganizer")
                    .setEmailAddress(GROUP))
            .execute();
    }
}

正如您从下面的输出中看到的,我的代码能够列出我的两个现有驱动器。第二个(id 编辑为 bbb)是我想与小组分享的那个。当我在创建权限时使用相同的 ID 时,API 响应 File not found:

drive list {"drives":[{"id":"aaa","kind":"drive#drive","name":"Manually Created Shared Drive"},
                      {"id":"bbb","kind":"drive#drive","name":"Shared Drive Created By Service Account"}],
            "kind":"drive#driveList"}

about to grant permission on id bbb

Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found
{
  "code" : 404,
  "errors" : [ {
    "domain"       : "global",
    "location"     : "fileId",
    "locationType" : "parameter",
    "message"      : "File not found: bbb.",
    "reason"       : "notFound"
  } ],
  "message" : "File not found: bbb."
}
  at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:150)
  at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
  at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
  at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.interceptResponse(AbstractGoogleClientRequest.java:444)
  at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1108)
  at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:542)
  at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:475)
  at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:592)
  at DriveQuickstart.main(DriveQuickstart.java:63)

为什么说File not found

我是否需要 setSupportsAllDrives(true) 才能包含 共享驱动器 而不是仅限于“我的驱动器”?如果有,在哪里?

文档说,2020 年 6 月 1 日之后,所有应用程序都假定支持共享驱动器。我使用的是截至今天(6 月 11 日)的最新可能依赖项,但所有这些都早于 6 月 1 日。这是我的 build.gradle 文件中的依赖项部分:

dependencies {
    compile 'com.google.api-client:google-api-client:1.30.9'
    compile 'com.google.auth:google-auth-library-oauth2-http:0.20.0'
    compile 'com.google.apis:google-api-services-drive:v3-rev20200413-1.30.9'
}

想通了:

        service
            .permissions()
            .create(DRIVE_ID,
                    new Permission()
                    .setType("group")
                    .setRole("fileOrganizer")
                    .setEmailAddress(GROUP))
            .setSupportsAllDrives(true) // <---- add this here
            .execute();

至于假设所有应用程序都支持共享驱动器的计划,是通过电子邮件从 Google 支持人员那里得到的:

There was a plan to activate this by default as of June 1st, but this is being reconsidered, and it will be necessary to add this parameter to access shared drives for the foreseeable future. Our documentation should be in the process of being updated.