【问题标题】:Why "File not found" when using fileId of shared drive in google-api-services-drive?为什么在google-api-services-drive中使用共享驱动器的fileId时“找不到文件”?
【发布时间】:2020-10-01 17:31:55
【问题描述】:

下面的代码试图为我之前创建的共享驱动器授予组 fileOrganizer 权限,该共享驱动器使用类似的代码和模拟同一用户的同一服务帐户。 (我已经用伪造的 DRIVE_ID 编辑了实际的 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$1.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'
}

【问题讨论】:

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


    【解决方案1】:

    想通了:

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

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

    计划从 6 月 1 日起默认激活此功能,但是这 正在重新考虑,有必要添加这个参数 在可预见的未来访问共享云端硬盘。我们的文档 应该正在更新中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-22
      • 1970-01-01
      • 1970-01-01
      • 2021-02-03
      相关资源
      最近更新 更多