通过 Drive API 删除文件失败并出现错误:此文件的权限不足 [英] Delete files via Drive API failed with error: Insufficient permissions for this file

查看:11
本文介绍了通过 Drive API 删除文件失败并出现错误:此文件的权限不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简介:Alice 准备了一个绑定到 Google 表格的应用程序脚本.该脚本从 Google Sheet 生成一个 Google Doc.该文档存储在 Alice 的 Google Drive 上.如果 Alice 生成了一个新的 Google Doc,那么旧的就会被删除.以下代码用于此.

函数 delteFile(myFileName) {var allFiles、idToDLET、myFolder、rtrnFromDLET、thisFile;myFolder = DriveApp.getFolderById("folder_id_is_here");allFiles = myFolder.getFilesByName(myFileName);而(allFiles.hasNext()){thisFile = allFiles.next();idToDLET = thisFile.getId();rtrnFromDLET = Drive.Files.remove(idToDLET);//对 drive.files.delete 的 API 调用失败并出现错误:此文件的权限不足};};

Alice 在 Google Cloud Platform 上与 Bob 分享了这个项目.

  1. 如果 Alice 生成了 Google Doc,那么 Bob 就无法成功执行上述代码.代码显示<块引用>

    对 drive.files.delete 的 API 调用失败并出现错误:此文件的权限不足

在这种情况下,Alice 可以成功执行上述代码.

  1. 如果 Bob 生成了 Google Doc,那么 Alice 就无法成功执行上述代码.代码显示相同的错误.Bob 在这种情况下可以成功执行上述代码.

问题:该项目在谷歌云平台上共享.爱丽丝还需要分享什么?

解决方案

解决这个问题的合适工具是 Google Shared Drive.但如果这在您的情况下不起作用,您可以创建自己的垃圾文件夹(例如ToTrash").然后授予可以垃圾的人对该文件夹的权限.您让人们使用脚本(见下文)将文件移动到您自己的ToTrash"文件夹,而不是丢弃.有时您会删除ToTrash"文件夹中的所有文件.为此,您还可以编写一个脚本(未提供但易于编写),例如每天自动触发一次.

//TRASHFOLDERID 是您自己创建的垃圾文件夹的 ID.//'file' 是你构造的文件对象//通过 id、name 或 FileIterator 获取常量垃圾文件夹 = DriveApp.getFolderById(TRASHFOLDERID);file.moveTo(trashFolder);

Intro: Alice prepared an app script that is bound to a Google Sheet. The script generates a Google Doc from the Google Sheet. The doc is stored on Alice's Google Drive. If Alice generates a new Google Doc, then the old one is deleted. The following code is used for that.

function delteFile(myFileName) {
  var allFiles, idToDLET, myFolder, rtrnFromDLET, thisFile;

  myFolder = DriveApp.getFolderById("folder_id_is_here");

  allFiles = myFolder.getFilesByName(myFileName);

  while (allFiles.hasNext()) {
    thisFile = allFiles.next();
    idToDLET = thisFile.getId();
    rtrnFromDLET = Drive.Files.remove(idToDLET); // API call to drive.files.delete failed with error: Insufficient permissions for this file
  };
};

Alice shared the project on Google Cloud Platform with Bob.

  1. If Alice generates a Google Doc, then Bob can not successfully execute the above code. The code shows

    API call to drive.files.delete failed with error: Insufficient permissions for this file

Alice can successfully execute the above code in this case.

  1. If Bob generates a Google Doc, then Alice can not successfully execute the above code. The code shows the same error. Bob can successfully execute the above code in this case.

Question: The project is shared on Google Cloud Platform. What else does Alice need to share?

解决方案

The proper tool for this problem is Google Shared Drive. But if that doesn't work in your situation, you can make your own trash folder (e.g. 'ToTrash'). Then give to the people that can trash, permission to this folder. Instead of trashing you let people use a script (see below) that moves files to your own 'ToTrash' folder. Once in a while you delete all files in the 'ToTrash' folder. For that you can also write a script (not provided but easy to write) that is automatically triggered for example once every day.

// TRASHFOLDERID is the Id of your own created trash folder.
// 'file' is a file object you constructed
// by getting it by id, name, or FileIterator

const trashFolder = DriveApp.getFolderById(TRASHFOLDERID);
file.moveTo(trashFolder);

这篇关于通过 Drive API 删除文件失败并出现错误:此文件的权限不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆