将文件权限更新为"所有者"失败,500内部错误

时间:2014-04-05 00:23:38

标签: google-drive-api

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

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

将管理员帐户的权限从“writer”更新为“owner”时,程序将返回500 Internal Error。任何帮助将不胜感激。谢谢!

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();
    }
}

1 个答案:

答案 0 :(得分:0)

意识到我本应该使用所有者的电子邮件来获取驱动服务。改变那一行后,这有效。感谢。

    Drive userDriveService = getDriveService(ORIGINAL_OWNER_EMAIL);