google-apps-script - 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?
解决方案
如果您使用的是:
. . . 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 并在需要时保存用户首选项。
推荐阅读
- javascript - 在网络上对齐吉他和弦
- php - 初学者 Hello world php 错误
- python - 尝试使用 toco 转换 TF 模型时出错
- laravel - 如何验证日期的表单请求?
- r - 如何在宽格式 data.frame 上运行 ANOVA?
- javascript - 在客户端过滤 HTML 表,但导出为 CSV 显示整个表
- java - 找到给定列表中的最小值?
- javascript - 在 node.js 中为注册用户创建配置文件
- ios - 在 SWIFT、GoogleToolboxForMac 中实现已弃用的 GTMURLBuilder 类
- python - 如何在不使用密钥的情况下访问节点属性?