Google Drive API teamdrives()。list()通过使用示例中的setQ参数来定位文件夹,但在编译时确实存在

时间:2018-03-01 14:23:50

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

我在Play Framework应用程序中使用Google Drive API库。

我正在尝试搜索Team Drive,检查文件夹是否存在,如果不存在则创建文件夹。我在FileList之前使用了TeamDriveList对象,但是使用FileList在团队驱动器中不断创建文件夹即使它存在 - 它也永远不会找到现有文件夹。它会一遍又一遍地复制文件夹。

所以,在阅读了几篇文章之后:

Iterate over Team Drive files Google Drive API JAVA

Google Drive API - Creates Duplicate Folders

好像我需要切换到TeamDriveList

我在Google的文档中使用此示例: https://developers.google.com/drive/v3/web/manage-teamdrives#managing_team_drives_for_domain_administrators

如您所见,它使用setQ执行搜索:

TeamDriveList result = driveService.teamdrives().list()
      .setQ("organizerCount = 0")
      .setFields("nextPageToken, teamDrives(id, name)")
      .setUseDomainAdminAccess(true)
      .setPageToken(pageToken)
      .execute();

但是,在我的代码中,当我尝试编译时,会抛出setQ不存在的错误。

这是我的代码:

public static String getSubfolderID(Drive service, String parentFolderID, String folderKeyToGet) {
    // We need to see if the folder exists based on the ID...
    String folderID = "";
    Boolean foundFolder = false;
    TeamDriveList result = null;
    File newFolder = null;

    // Set the drive query...
    String driveQuery = "mimeType='application/vnd.google-apps.folder' and '" + parentFolderID
            + "' in parents and name contains '" + folderKeyToGet
            + "' and trashed=false";

    try {
        result = service.teamdrives().list()
                .setQ(driveQuery)
                .execute();
    } catch (IOException e) {
        e.printStackTrace();
    }
    for (TeamDrive folder : result.getTeamDrives()) {
        System.out.printf("Found folder: %s (%s)\n", folder.getName(), folder.getId());
        foundFolder = true;
        folderID = folder.getId();
    }
    if (foundFolder != true) {
        // Need to create the folder...
        File fileMetadata = new File();
        fileMetadata.setName(folderKeyToGet);
        fileMetadata.setTeamDriveId(parentFolderID);
        fileMetadata.set("supportsTeamDrives", true);
        fileMetadata.setMimeType("application/vnd.google-apps.folder");
        fileMetadata.setParents(Collections.singletonList(parentFolderID));

        try {
            newFolder = service.files().create(fileMetadata)
                    .setSupportsTeamDrives(true)
                    .setFields("id, parents")
                    .execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // Send back the folder ID...
        folderID = newFolder.getId();
        System.out.println("Folder ID: " + newFolder.getId());
    }

    return folderID;
}

有问题的代码行是:

    result = service.teamdrives().list()
            .setQ(driveQuery)
            .execute();

如果不存在,我该如何使用此查询进行搜索:

String driveQuery = "mimeType='application/vnd.google-apps.folder' and '" + parentFolderID
        + "' in parents and name contains '" + folderKeyToGet
        + "' and trashed=false";

我正在寻找的文件夹,并确保我找到该文件夹​​,以免造成重复?

感谢帮助。

0 个答案:

没有答案