添加和移除具有权限的记事协作者

记事的创建者即为所有者。所有者可以添加协作者,并向单个用户或群组授予相应权限。协作者获得记事的修改权限。您还可以使用 API 撤消协作者权限。本页面介绍了如何使用权限添加和删除协作者。

添加了添加协作者的权限

以下示例展示了如何使用用户或群组的权限向记事添加协作人员:

REST

使用分配了权限的父记事和 permission 调用 notes.permissions.batchCreate()。请注意,只能为成员(用户、群组)分配 WRITER 角色。

Java

/**
 * Grants write access to a user and to a Google group for the given note.
 *
 * @param note The note whose permissions will be updated.
 * @param userEmail Email address of the user that will be added to the
 *     permissions of the note.
 * @param groupEmail Email address of the Google Groups that will be
 *     added to the permissions of the note.
 * @throws IOException
 * @return The response of the create permissions request.
 */
private BatchCreatePermissionsResponse addPermissions(
    Note note, String userEmail, String groupEmail) throws IOException {
  String noteName = note.getName();
  CreatePermissionRequest userPermission =
      new CreatePermissionRequest()
          .setParent(noteName)
          .setPermission(new Permission().setEmail(userEmail).setRole("WRITER"));

  CreatePermissionRequest groupPermission =
      new CreatePermissionRequest()
          .setParent(noteName)
          .setPermission(new Permission().setEmail(groupEmail).setRole("WRITER"));

  BatchCreatePermissionsRequest batchCreatePermissionsRequest =
      new BatchCreatePermissionsRequest()
          .setRequests(Arrays.asList(userPermission, groupPermission));

  return keepService
      .notes()
      .permissions()
      .batchCreate(noteName, batchCreatePermissionsRequest)
      .execute();
}

撤消权限以移除协作者

以下示例展示了如何使用权限从记事中移除所有协作者:

REST

使用已分配权限的父级笔记和 permission 调用 notes.permissions.batchDelete()

Java

/**
 * Deletes all permissions of a given note excluding the owner. The owner
 * can't be removed from a note's permissions.
 *
 * @param note The note whose permissions will be deleted.
 * @throws IOException
 */
private void deletePermissions(Note note) throws IOException {
  List<Permission> notePermissions =
      keepService.notes().get(note.getName()).execute().getPermissions();

  // List of users, groups or families that will be deleted from the
  // permissions of the note.
  List<String> permissionsToDelete = new ArrayList<>();

  for (Permission permission : notePermissions) {
    // The note owner can't be removed from the permissions. Trying to
    // remove the owner causes an exception.
    if (!permission.getRole().equals("OWNER")) {
      permissionsToDelete.add(permission.getName());
    }
  }

  keepService
      .notes()
      .permissions()
      .batchDelete(
          note.getName(),
          new BatchDeletePermissionsRequest().setNames(permissionsToDelete))
      .execute();
}