首页 > 解决方案 > How to request user permission to read a file in GAS?

问题描述

I am working on a Google Apps Script add-on for Sheets which has been verified for the drive.file scope. At one point I need to read the parent folder of the current spreadsheet so I can create files in it. Is it possible to do this without having the drive.readonly scope? Could I request access from the user solely to that folder? If so, how would I do this?

标签: google-apps-scriptgoogle-drive-apigoogle-oauthadd-on

解决方案


如果您使用的是:
. . . googleapis.com/auth/drive.file
范围,那么您不需要也有drive.readonly范围。插件可以通过让用户使用 Google 文件/文件夹选择器来访问特定文件夹。因此,用户需要在代码运行之前使用选择器来访问该文件夹。未经用户授权,插件无法访问文件夹。

  • 在 Google Cloud Platform 项目中启用 Google Picker API
  • 获取选择器的开发者密钥
  • 将 Google 选择器加载到 HTML 中的脚本标记中: <script src="https://apis.google.com/js/api.js?onload=onApiLoad"></script><!-- This is for the file picker - -->
  • 将您的客户端代码与选择器代码集成。例如,您打开选择器的按钮
  • 配置选择器设置。对文件夹使用 mime 类型: view.setMode(google.picker.DocsViewMode.LIST).setMimeTypes('application/vnd.google-apps.folder');
  • 处理从选取器返回的值以获取文件夹 ID 并在需要时保存用户首选项。

请参阅:
https ://developers.google.com/picker/docs


推荐阅读