【问题标题】:using google docs api from within google apps script在 google 应用程序脚本中使用 google docs api
【发布时间】:2020-02-26 11:00:42
【问题描述】:

我正在尝试在 google apps 脚本中使用来自 google docs API 的以下代码进行测试:

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,这似乎是同一件事(或者是同一件事)?

无论如何,当我转到该链接时,它是一个错误(预期),因为没有真正的“项目”要激活,因为谷歌应用程序脚本在幕后做了很多事情来使 api 工作。

那么如何绕过谷歌应用脚​​本中的这个错误呢?顺便说一句,我的清单文件如下所示:

{
  "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"
}

所以我认为范围是包括在内的。

有什么想法吗?

【问题讨论】:

    标签: javascript http google-apps-script fetch google-docs


    【解决方案1】:

    作为其他方法,如何在 Advanced Google services 中启用 Docs API?

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

    模式一:

    在此模式中,您的脚本通过修改来使用。

    修改点:

    • 请将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() 检索到的访问令牌可用于此目的。

    模式 2:

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

    示例脚本:

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

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

    注意:

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

    参考资料:

    【讨论】:

      【解决方案2】:

      你需要

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-23
        • 1970-01-01
        相关资源
        最近更新 更多