创建一个 Google App Script web 应用程序以将文件上传到最小范围的 google 驱动器

Creating a Google App Script web app to upload files to google drive with minimum scope

我想创建一个 Google App Script 网络应用程序以允许用户将文件上传 到我的 google 驱动器 .

问题是我检查了驱动器作用域列表 here,

为了执行上传操作,用户必须审核此范围

https://www.googleapis.com/auth/drive

它带有一个可怕的描述 See, edit, create and delete all of your Google Drive files 太强大了。

有没有什么方法可以在较小的范围内执行上传操作?

非常感谢。

以你的情况为例,以下范围如何?

https://www.googleapis.com/auth/drive.file

官方文档是这样说的。 Ref

Per-file access to files created or opened by the app. File authorization is granted on a per-user basis and is revoked when the user deauthorizes the app.

在此范围内,只能访问此应用程序创建的文件和文件夹。

参考:

已添加:

根据您的以下回复,

Thank you for replying too, I have an update. When I run the code in the editor , it throws an error : ' { [Exception: You do not have permission to call DriveApp.Folder.createFile. Required permissions: googleapis.com/auth/drive] name: 'Exception' }' So I guess there is no solution to my problem T.T

我了解到您正在使用 createFile 和云端硬盘服务 (DriveApp)。在这种情况下,需要使用 https://www.googleapis.com/auth/drive 的范围。这似乎是当前的规范。为了使用 https://www.googleapis.com/auth/drive.file 的范围,在 Advanced Google 服务中使用 Drive API 怎么样?这样,您可以通过设置为清单文件 appsscript.json 来使用范围。

首先,请将https://www.googleapis.com/auth/drive.file作为"oauthScopes": ["https://www.googleapis.com/auth/drive.file"]添加到appsscript.json的清单文件中。 Ref

虽然我不确定您的实际脚本,但创建电子表格的示例脚本如下。

示例脚本:

在您使用此脚本之前,please enable Drive API at Advanced Google services

function myFunction() {
  const obj = Drive.Files.insert({title: "sampleSpreadsheet", mimeType: MimeType.GOOGLE_SHEETS, parents: [{id: "###folderId###"}]});
  console.log(obj.id)
}
  • 当您 运行 这个脚本时,新的电子表格被创建到 folderId https://www.googleapis.com/auth/drive.file

注:

  • 如果您需要使用其他范围,请将它们添加到oauthScopes

参考文献: