首页 > 解决方案 > 将 Google Drive 文件夹创建到另一个文件夹时出错

问题描述

我需要将文件夹创建GDrive到另一个GDrive文件夹中。

有代码:

private static String createDriveFolder(String folderName, String mainFolder, Drive service) throws IOException {
    File fileMetadata = new File();
    fileMetadata.setName(folderName);
    fileMetadata.setParents(Collections.singletonList(mainFolder));
    fileMetadata.setMimeType("application/vnd.google-apps.folder");

    File file = service.files().create(fileMetadata)
            .setFields("id, parent")
            .execute();
    System.out.println("Folder ID: " + file.getId());
    return file.getId();
}

我得到这个错误...

Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
  "code" : 400,
  "errors" : [ {
    "domain" : "global",
    "location" : "fields",
    "locationType" : "parameter",
    "message" : "Invalid field selection parent",
    "reason" : "invalidParameter"
  } ],
  "message" : "Invalid field selection parent"
}

请帮我...

标签: javagoogle-drive-api

解决方案


这个改装怎么样?我认为从错误消息来看,问题的原因是由于fields.

从:

.setFields("id, parent")

至:

.setFields("id, parents")

笔记:

  • 请修改parentparents. 在 Google Drive,在当前阶段,一个文件和文件夹有多个父级。所以parents被使用。但是我们可以看到下面的公告。参考

    从 2020 年 9 月 30 日开始,将无法再将一个项目放置在多个文件夹中;每个项目将只有一个位置。在新模型中,可以使用快捷方式来组织多个层次结构中的内容。Drive 文件夹结构和共享模型的这种简化将导致某些 Google Drive API 端点的行为方式发生变化。

    • 我不确定是否parents会更改为parent. 因此,关于这个,我想确认 2020 年 9 月 30 日之后 Drive API 的更改。

参考:


推荐阅读