using google docs api from within google apps script

别等时光非礼了梦想. 提交于 2020-03-24 00:10:02

问题


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)

(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"
          }
        ]
      }
    ]
  }
}

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

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.

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.

Any ideas?


回答1:


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

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.

Pattern 1:

In this pattern, your script is used by modifying.

Modification point:

  • Please modify body to payload.

Modified script:

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)
  • By enabling Docs API at Advanced Google services, the access token retrieved by ScriptApp.getOAuthToken() can be used for this.

Pattern 2:

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

Sample script:

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)

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).

References:

  • Class UrlFetchApp
  • Advanced Google services
  • Method: documents.batchUpdate



回答2:


You need to

  • Switch to a different Google Cloud Project and

  • Enable the docs api for that project



来源:https://stackoverflow.com/questions/60387167/using-google-docs-api-from-within-google-apps-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!