从google apps脚本中使用google docs api [英] using google docs api from within google apps script

查看:136
本文介绍了从google apps脚本中使用google docs api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用Google Apps脚本中google docs API中的以下代码进行测试:

I'm trying to use the following code from the google docs API from within google apps script for testing purposes:

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令牌时,结果如下);但是,我得到一个错误:

(Also, when I try this in the browser manually entering in the oauth token, same result as below); however, I get an error:

{
  "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 Apps脚本中使用DocumentApp,这似乎是同一回事(或者是这样)?

even though I can use the DocumentApp from within google apps script, which is seemingly the same thing (or is it)?

无论如何,当我转到该链接时,这是一个错误(可能是错误的),因为没有真正的项目"可激活,因为Google Apps脚本在幕后做了很多事情来使api正常工作.

Anyway, when I go to that link its an error (expectedly), since there is no real "project" to activate, as the google apps script does a lot of behind the scenes stuff to make the api work.

那么我该如何绕过Google Apps脚本中的此错误?我的清单文件的内容如下所示:

So how can I bypass this error in google apps script? My manifest file reads like this BTW:

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

所以我认为范围已包括在内.

So I think the scope is included.

有什么想法吗?

推荐答案

作为另一种方法,如何在Advanced Google服务中启用Docs API?

As other method, how about enabling Docs API at Advanced Google services?

为此,首先,请请在高级Google上启用Docs API服务.这样,您可以通过直接请求Docs API的端点来使用Docs API.在这种情况下,您无需链接GAS项目和Google Could Project即可使用Docs API.

For this, at first, please enable Docs API at Advanced Google services. By this, you can use the Docs API by directly requesting to the endpoint of Docs API. In this case, you are not required to use the Docs API without linking GAS project and Google Could Project.

在这种模式下,您的脚本用于修改.

In this pattern, your script is used by modifying.

  • 请将body修改为payload.
  • Please modify body to 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()检索到的访问令牌可用于此目的.
    • By enabling Docs API at Advanced Google services, the access token retrieved by ScriptApp.getOAuthToken() can be used for this.
    • 在这种模式下,将使用Google高级服务的Docs API.示例脚本如下.

      In this pattern, Docs API of Advanced Google services is used. The sample script is as follows.

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

      In this case, your request body and document ID can be used. And also, you are not set the header for authorizing.

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

      注意:

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

        • In this case, the required scopes are automatically installed. But if you want to control the scopes, please set them to the manifest file (appsscript.json).
          • Class UrlFetchApp
          • Advanced Google services
          • Method: documents.batchUpdate

          这篇关于从google apps脚本中使用google docs api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆