【问题标题】:Why can't I open a file by ID that I have installed my add-on in?为什么我无法按 ID 打开已安装插件的文件?
【发布时间】:2021-08-27 18:36:09
【问题描述】:

根据 Apps 脚本documentation

openById(id)

打开具有给定 ID 的电子表格。 ...

...

授权

使用此方法的脚本需要具有以下一个或多个范围的授权:

我想将openByIdspreadsheets.currentonly 范围一起使用,因为它适合我的需要。但是,即使将范围添加到 appsscript.json 清单后,以下代码对我来说仍然失败:

const sheet_id = SpreadsheetApp.getActiveSpreadsheet().getId();
SpreadsheetApp.openById(sheet_id);

例外:您无权调用 SpreadsheetApp.openById。所需权限:https://www.googleapis.com/auth/spreadsheets

我知道我可以只使用https://www.googleapis.com/auth/spreadsheets 范围,但是由于文档说我应该能够使用spreadsheets.currentonly,这就是我想要做的。我在这里做错了什么?


更新:

This answer 是相关的,但建议的解决方法对我不起作用。我上面分享的代码 sn-p 是在脚本编辑器中执行的。我也不打算创建自定义函数。我打算在 Web 应用脚本的 doPost(e) 函数中运行此代码。

【问题讨论】:

    标签: google-apps-script post google-sheets web-applications scopes


    【解决方案1】:

    OAuth 范围 ID 有时是可访问的链接,可以提供有关范围的有用信息。

    导航到https://www.googleapis.com/auth/spreadsheets.currentonly后我收到以下消息:

    您收到此错误的原因可能是您输入的 OAuth2 范围名称无效,或者它引用了此旧 API 域之外的较新范围。

    此 API 是在范围名称格式尚未标准化的时候构建的。现在不再是这种情况,所有有效的范围名称(旧的和新的)都在https://developers.google.com/identity/protocols/oauth2/scopes 中编目。使用该网页(手动)查找与您尝试调用的 API 关联的范围名称,并使用它来制作您的 OAuth2 请求。

    最终,我找到了here,它列出了 Sheets API 的以下范围:

    • https://www.googleapis.com/auth/drive
    • https://www.googleapis.com/auth/drive.file
    • https://www.googleapis.com/auth/drive.readonly
    • https://www.googleapis.com/auth/spreadsheets
    • https://www.googleapis.com/auth/spreadsheets.readonly

    https://www.googleapis.com/auth/spreadsheets.currentonly 范围似乎不再有效。我怀疑这是由于 Sheets API 的第 3 版在 2021 年 8 月 2 日(在撰写本文时几周前)达到其日落日期的结果。可能范围没有延续到 Sheets API 的第 4 版。

    这个问题当然值得进一步调查。我建议您使用 Google 的问题跟踪器通过以下链接报告问题:

    https://issuetracker.google.com/components/191608.

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-16
    • 2015-07-06
    • 2013-06-06
    • 2016-04-14
    • 1970-01-01
    • 2018-02-14
    相关资源
    最近更新 更多