错误:尝试删除文件或文件夹的权限时没有此类用户

问题描述 投票:0回答:2

我正在使用 google-app-scripts 撤销对谷歌驱动器文件和文件夹的权限,我循环遍历我的谷歌驱动器文件和文件夹,当我发现用户有权访问该文件时,我尝试撤销它,但随后异常:“没有这样的用户”发生,我找不到任何原因为什么会发生这种情况。

function run() {
    var emailsToRevoke = ["EmailA", "EmailB"];
    revoke(emailsToRevoke);
}

function revoke(emails){
  var files = DriveApp.getFiles();
  var folders = DriveApp.getFolders();

  emails.forEach(function(email){
    try {
      while(files.hasNext()) {
        var file = files.next();

        if(file.getAccess(email) !== DriveApp.Permission.NONE) {
          file.revokePermissions(email)
        }

        var editors = file.getEditors();
        var viewers = file.getViewers();

        console.log("Checking Permission for: " + file)

        for (var i = 0; i < editors.length; i++) {
          if(editors[i].getEmail() === email) {
            console.log("Revoking Permission to " + file.getName() + " for: " + email )
            file.removeEditor(email)
          };
        }

        for (var i = 0; i < viewers.length; i++) {
          if(viewers[i].getEmail() === email) {
            console.log("Revoking Permission to " + file.getName() + " for: " + email )
            file.removeViewer(email)
          };
        }

        doubleCheckUser(file, email)
      }

      while(folders.hasNext()) {
        var folder = folders.next();


        if(folder.getAccess(email) !== DriveApp.Permission.NONE) {
          folder.revokePermissions(email)
        }

        var editors = folder.getEditors();
        var viewers = folder.getViewers();

        console.log("Checking Permission for: " + folder)

        for (var i = 0; i < editors.length; i++) {
          if(editors[i].getEmail() === email) {
            console.log("Revoking Permission to " + folder.getName() + " for: " + email )
            folder.removeEditor(email)
          };
        }

        for (var i = 0; i < viewers.length; i++) {
          if(viewers[i].getEmail() === email) {
            console.log("Revoking Permission to " + folder.getName() + " for: " + email )
            folder.removeViewer(email)
          };
        }

        doubleCheckUser(folder, email);
      }
    } catch(error) {
      console.error("Error During Revoking Access Process: " + error)
    }   
  });
}

function doubleCheckUser(objectToCheck, email) {
  try {
    var access = objectToCheck.getAccess(email)
    if(access !== DriveApp.Permission.NONE) {
      console.warn("Access to: " + objectToCheck.getName() + " has not been revoked properly. Current Access: " + access)
    }
  } catch(error) {
    console.error("Error Double Checking If User: " + email + " Was Removed Properly From: " + objectToCheck.getName())
  }
}

我确信电子邮件可以访问文件,因为我将其登录到 doubleCheckUser() 中,我也确信电子邮件是有效的。

其触发方式很简单,只需用户将电子邮件添加到 emailsToRevoke 数组,然后在 google 脚本 UI 中选择运行函数并按运行即可。

我的问题是,我做错了什么以及如何解决这个问题?归根结底,我需要的只是撤销访问权限,所以如果您知道更好的方法,请不要犹豫。

编辑---
请注意,驱动器中有很多文件(绝对超过 1000 个文件和文件夹)

google-apps-script google-drive-api
2个回答
0
投票

我刚刚遇到了同样的问题,似乎虽然文档支持通过电子邮件地址删除用户,但代码需要驱动器用户对象(而不是电子邮件地址字符串...)我通过使用以下代码解决了它,希望这对某人有帮助:

const fileIds = ["abc123","123abc"]//array of file ids to have permissions revoked from
var emailAddr = "email address you want to revoke"

revoke(emailAddr)

function revoke(em){
  Logger.log("Revoke "+em)
  for (var f in fileIds){
    Logger.log("FILE ID "+f)
    for (var loop=0; loop<1;loop++){ // sometimes users are repeated - this step ensures the list is checked again if the target email address is found
      for(i in DriveApp.getFileById(fileIds[f]).getViewers()){
        Logger.log(DriveApp.getFileById(fileIds[f]).getViewers()[i].getEmail())
        if(DriveApp.getFileById(fileIds[f]).getViewers()[i].getEmail()==em){
          Logger.log("rem "+DriveApp.getFileById(fileIds[f]).getViewers()[i].getEmail())
          DriveApp.getFileById(fileIds[f]).revokePermissions(DriveApp.getFileById(fileIds[f]).getViewers()[i])
          loop-- //target email address found, iterate loop one more time to check again
          break
        }
      }
    }
  }
}

山姆


0
投票

我一直收到此错误。大多数时候,我只需再次运行脚本,错误就会消失。看起来像是 Google 端的一个错误。

对于包含大量文件的目录,我会遇到这种情况。再次运行的问题是,脚本运行的时间越长,遇到错误的可能性就越大。我已采取在子目录上运行脚本的方法(并在遇到错误时重新运行)。

有人尝试捕获这个异常吗?

© www.soinside.com 2019 - 2024. All rights reserved.