谷歌云端硬盘 API - 将文件权限更新为"owner"失败,并显示 500 内部错误



对于我们的域,需要将特定文件夹中的文件从一个用户传输到管理员帐户。 管理员帐户对所有文件具有"编写者"角色。 这两个用户位于同一域中。

我使用域范围的委派作为授权方法。

将管理员帐户的权限从"编写者"更新为"所有者"时,程序返回 500 内部错误。 任何帮助将不胜感激。 谢谢!

public static Drive getDriveService(String userEmail) throws GeneralSecurityException,IOException, URISyntaxException {
  HttpTransport httpTransport = new NetHttpTransport();
  JacksonFactory jsonFactory = new JacksonFactory();
  Collection<String> scope = new ArrayList<String>();
  scope.add(DriveScopes.DRIVE);
  GoogleCredential credential = new GoogleCredential.Builder()
      .setTransport(httpTransport)
      .setJsonFactory(jsonFactory)
      .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
      .setServiceAccountScopes(scope)
      .setServiceAccountUser(userEmail)
      .setServiceAccountPrivateKeyFromP12File(
          new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))
      .build();
  Drive service = new Drive.Builder(httpTransport, jsonFactory, null)
      .setHttpRequestInitializer(credential).build();
  return service;
}
public static void main(String[] args) {
    try {
        Drive userDriveService = getDriveService(ADMIN_ACCT_EMAIL);
        ChildList list = userDriveService.children().list(folderId).setQ("not '" + ADMIN_ACCT_EMAIL + "' in owners").execute();
        java.util.List<ChildReference> children = list.getItems();
                //Simplified for testing
        ChildReference child = children.get(0);
        String fileId = child.getId();
        String permId = userDriveService.permissions().getIdForEmail(ADMIN_ACCT_EMAIL).execute().getId();
        Permission perm = userDriveService.permissions().get(fileId, permId).execute();
        perm.setRole("owner");
        userDriveService.permissions().update(fileId, permId, perm).setTransferOwnership(true).execute();
    } catch (GeneralSecurityException | IOException | URISyntaxException e) {
        e.printStackTrace();
    }
}
意识到

我应该使用所有者的电子邮件来获取驱动器服务。 这在更改了那一行后起作用。 谢谢。

    Drive userDriveService = getDriveService(ORIGINAL_OWNER_EMAIL);

最新更新