您如何通过 Google Apps 脚本访问 Google 表格 API?

How do you access the Google Sheets API via a Google Apps Script?

我正在尝试通过 Google Apps 脚本调用 Google 表格 API。例如,我已按照此处提供的说明进行操作:

https://developers.google.com/sheets/api/quickstart/js

我已经按照其他地方的建议通过我的 Google 云平台项目生成了客户端 ID 和 API 密钥。

现在,我创建了一个包含两个文件的 google 应用程序脚本,Code.gs 文件和一个名为 index.html 的文件,其中包含从 Google 工作表 API 上面的快速入门(插入了我的客户端 ID 和 API)。

测试部署时,我收到以下错误消息:

{
  "error": "idpiframe_initialization_failed",
  "details": "Not a valid origin for the client: https://<some numbers>-script.googleusercontent.com has not been registered for client ID <MY_CLIENT_ID>. Please go to https://console.developers.google.com/ and register this origin for your project's client ID."
}

我已将 script.google.com 注册为我项目的来源。

我做错了什么吗?我将如何从这里开始克服这个错误?

如果您想在 Apps 脚本中使用 Sheets API,则必须使用表格高级服务。

为此,您必须转到 Services +,然后添加 Google Sheets API:

由于这是一项高级服务,授权流程将由 Google 处理,因此您不必提供 clientIdAPI key

参考