Search code examples
javascripthttpgoogle-apps-scriptfetchgoogle-docs

using google docs api from within google apps script


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?


Solution

  • 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: