在 google 应用程序脚本中使用 google 文档 api

using google docs api from within google apps script

我正在尝试使用 google 文档 API 中 google 应用程序脚本中的以下代码进行测试:

var f = UrlFetchApp.fetch("https://docs.googleapis.com/v1/documents/1ys6KIY1XOhPHrgQBJ4XxR1WDl0etZdmR9R48h2sP2cc:batchUpdate", {
    method:"post",
    body: JSON.stringify({
      "requests": [
        {
          "deleteContentRange": {
            "range": {
              "startIndex": 1,
              "endIndex": 80
            }
          }
        }
      ]
    }),
    headers: {
      Authorization:"Bearer " + ScriptApp.getOAuthToken(),
      "Content-Type": "application/json"
    }
  })
  Logger.log(f)

(另外,当我在浏览器中尝试此操作时手动输入 oauth 令牌,结果与下面相同);但是,我得到一个错误:

{
  "error": {
    "code": 403,
    "message": "Google Docs API has not been used in project 861341326257 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/docs.googleapis.com/overview?project=861341326257 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.",
    "status": "PERMISSION_DENIED",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.Help",
        "links": [
          {
            "description": "Google developers console API activation",
            "url": "https://console.developers.google.com/apis/api/docs.googleapis.com/overview?project=861341326257"
          }
        ]
      }
    ]
  }
}

即使我可以在 google 应用程序脚本中使用 DocumentApp,这看起来是一回事(或者是)?

无论如何,当我转到那个 link 时,它是一个错误(预期),因为没有真正的 "project" 可以激活,因为 google 应用程序脚本做了很多api 工作的幕后花絮。

那么如何绕过 google 应用程序脚本中的这个错误?我的清单文件是这样的顺便说一句:

{
  "timeZone": "America/New_York",
  "dependencies": {
  },
  "oauthScopes": [
    "https://www.googleapis.com/auth/documents",
    "https://www.googleapis.com/auth/drive",
    "https://www.googleapis.com/auth/script.external_request"
  ],
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8"
}

所以我认为范围是包含的。

有什么想法吗?

你需要

作为其他方法,在高级 Google 服务中启用 Docs API 怎么样?

为此,首先,please enable Docs API at Advanced Google services.这样,您可以通过直接向 Docs API 的端点请求来使用 Docs API。在这种情况下,您不需要在不链接 GAS 项目和 Google Could 项目的情况下使用文档 API。

模式 1:

在这个模式中,你的脚本是通过修改来使用的。

修改点:

  • 请将body修改为payload

修改后的脚本:

var f = UrlFetchApp.fetch("https://docs.googleapis.com/v1/documents/1ys6KIY1XOhPHrgQBJ4XxR1WDl0etZdmR9R48h2sP2cc:batchUpdate", {
  method:"post",
  contentType: "application/json",
  payload: JSON.stringify({
    "requests": [
      {
        "deleteContentRange": {
          "range": {
            "startIndex": 1,
            "endIndex": 80
          }
        }
      }
    ]
  }),
  headers: {Authorization:"Bearer " + ScriptApp.getOAuthToken()}
})
Logger.log(f)
  • 通过在高级 Google 服务中启用 Docs API,ScriptApp.getOAuthToken() 检索到的访问令牌可用于此目的。

模式二:

在此模式中,使用了高级 Google 服务的文档 API。示例脚本如下

示例脚本:

在这种情况下,可以使用您的请求 body 和文档 ID。而且,您没有设置 header 进行授权。

var documentId = "1ys6KIY1XOhPHrgQBJ4XxR1WDl0etZdmR9R48h2sP2cc";
var resource = {
  "requests": [
    {
      "deleteContentRange": {
        "range": {
          "startIndex": 1,
          "endIndex": 80
        }
      }
    }
  ]
};
var res = Docs.Documents.batchUpdate(resource, documentId);
Logger.log(res)

注:

  • 在这种情况下,会自动安装所需的范围。但是如果你想控制范围,请将它们设置到清单文件 (appsscript.json)。

参考文献: