Google Drive API - 如何将子文件夹放入Team Drive

时间:2017-10-17 00:28:48

标签: google-drive-api google-drive-realtime-api

我正在使用Google Drive APIJAVA开发小型系统, 我想知道TeamDrive API中的Google Drive APIs

首先,我创建了团队驱动文件夹,

创建团队驱动器

public static void createTeamDrive(String accessToken) throws IOException {
    long startTime = System.currentTimeMillis();
    TeamDrive teamDriveMetadata = new TeamDrive();
    teamDriveMetadata.setName("Team Drive API Test");
    String requestId = UUID.randomUUID().toString();
    TeamDrive teamDrive = getDriveService(accessToken).teamdrives().insert(requestId, teamDriveMetadata).execute();
    System.out.println("[Create TeamDrive] execution time : " + (System.currentTimeMillis() - startTime));
}

然后我分享了用户。

分享用户

public static Permission ShareUser(String teamDriveFolderId, String googleMail, String accessToken) {
    long startTime = System.currentTimeMillis();
    Permission userPermission = new Permission().setRole("reader")
            .setType("user").setEmailAddress(googleMail)
            .setValue(googleMail).setWithLink(false);
    try {
        userPermission = getDriveService(accessToken).permissions().insert(teamDriveFolderId, userPermission)
                .setSupportsTeamDrives(true).execute();
        System.out.println(userPermission.toPrettyString());
    } catch (IOException e) {
        System.out.println(e);
    }
    System.out.println("[Give Permission] execution time : " + (System.currentTimeMillis() - startTime));
    return userPermission;
}

然后我尝试使用google-drive库多次创建子文件夹, 但是我没能连续创建子文件夹。

创建子文件夹

public static void createSubFolder(String accessToken, String teamDriveFolderId) throws IOException {
    long startTime = System.currentTimeMillis();
    File fileMetaData = new File();
    fileMetaData.setTitle("SubFolder Title ");
    fileMetaData.setMimeType("application/vnd.google-apps.folder");
    fileMetaData.setTeamDriveId(teamDriveFolderId);
    fileMetaData.setParents(Arrays.asList(new ParentReference().setId(teamDriveFolderId)));
    fileMetaData.set("supportsTeamDrives", true);
    fileMetaData.set("fields", "id");
    File file = null;
    try {
        file = getDriveService(accessToken).files().insert(fileMetaData).execute();
        System.out.println(file.getId());
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("[Create Sub folder] execution time : " + (System.currentTimeMillis() - startTime));
}

当我调用'createSubfolderFunction'时 我可以得到这样的404响应。

com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found
{
  "code" : 404,
  "errors" : [ {
    "domain" : "global",
    "location" : "file",
    "locationType" : "other",
    "message" : "File not found: 0AHGrQOmlzQZUUk9PVA",
    "reason" : "notFound"
  } ],
  "message" : "File not found: 0AHGrQOmlzQZUUk9PVA"
}
    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146)

'0AHGrQOmlzQZUUk9PVA' is the exact result from createTeamDrive function.

我提到了这个问题。enter link description here

请查看我的源代码并指出出了什么问题。

2 个答案:

答案 0 :(得分:0)

我自己发现了问题, 我在使用insert方法时应该添加setSupportTeamDrive(true)。

 file = getDriveService(accessToken).files().insert(fileMetaData).setSupportsTeamDrives(true).execute();

答案 1 :(得分:0)

我的文件夹 ID 有问题,请检查您的 google 驱动器的父文件夹 ID 是否正确 $folderId = '0AIuzzEYPQu9CUk9PVA'; 打开父文件夹后,id 出现在谷歌驱动器文件夹 URL 中