我想在google drive上公开我的文件

时间:2017-03-19 08:49:10

标签: android google-drive-api google-drive-android-api

我正在尝试上传,然后在Android上的谷歌驱动器上共享文件。我能够获得其文件ID和资源ID。现在我想公开它,以便有链接的用户可以访问它。我如何以编程方式执行此操作?

1 个答案:

答案 0 :(得分:0)

使用Permissions.create并将文件类型设置为“nobody”。      以下是Manage Sharing in Drive指南中的代码段:

    String fileId = "1sTWaJ_j7PkjzaBWtNc3IzovK5hQf21FbOw9yLeeLPNQ";
JsonBatchCallback<Permission> callback = new JsonBatchCallback<Permission>() {
    @Override
    public void onFailure(GoogleJsonError e,
                          HttpHeaders responseHeaders)
            throws IOException {
        // Handle error
        System.err.println(e.getMessage());
    }

    @Override
    public void onSuccess(Permission permission,
                          HttpHeaders responseHeaders)
            throws IOException {
        System.out.println("Permission ID: " + permission.getId());
    }
};
BatchRequest batch = driveService.batch();
Permission userPermission = new Permission()
        .setType("user")
        .setRole("writer")
        .setEmailAddress("user@example.com");
driveService.permissions().create(fileId, userPermission)
        .setFields("id")
        .queue(batch, callback);

Permission domainPermission = new Permission()
        .setType("domain")
        .setRole("reader")
        .setDomain("example.com");
driveService.permissions().create(fileId, domainPermission)
        .setFields("id")
        .queue(batch, callback);

batch.execute();