3

I would like to edit my Google Doc using a Google AppScript function externally via a Google Chrome extension. To achieve this I have performed the following steps:

  1. setup the script via Google Docs Tools > Script Editor
  2. implementation of the dummy function "myFunction" with the body
var body = DocumentApp.openById(<docId>).getBody()
body.appendParagraph("Another paragraph.")
  1. adding the Google Cloud Project ID in the script editor under Resources > Cloud Platform Project (the ID is identical to the ID I use in the Chrome Extension; the project also has Script API permissions).
  2. publishing the script via Publish > Executable API in the script.
  3. adding oauth2 in the manifest.json of the Chrome Extension.
  4. initialize the gapi JavascriptBrowser client in a background script in the Chrome Extension with valid apiKey and discoveryDoc.
// gapi is available in global scope
gapi.load('client', async () => {
    await gapi.client.init({
        apiKey: '<apiKey>',
        discoveryDocs: ['https://script.googleapis.com/$discovery/rest?version=v1'],
    })

    chrome.identity.getAuthToken({ interactive: true }, async (token) => {
        // set my valid oauth token here
        gapi.auth.setToken({
            access_token: token,
        })
    })
})
  1. calling scripts.run:
await gapi.client.script.scripts.run({
    scriptId: <scriptId>,
    resource: {
        function: 'myFunction'
    }
})

Unfortunately, I get the following error message when running the script:

{
    code: 403,
    message: "Requests to this API script method google.discovery.Discovery.GetDiscoveryRest are blocked."
    status: "PERMISSION_DENIED"
}

To make sure I didn't miss anything, I tried to call the Google Script API via Postman. I used the endpoint https://script.googleapis.com/v1/scripts/{scriptId}:run and the valid oauth2 token of the Chrome Extension. This worked successfully.

Additionally, I tried to change the `discoveryDoc' URL in the initialization function on https://www.googleapis.com/discovery/v1/apis/script/v1/rest?version=v1 - here a similar error occurs at a later time.

{
    code: 403,
    message: "Requests to this API script method google.apps.script.v1.ScriptExecution.Execute are blocked"
    status: "PERMISSION_DENIED"
}

I am at a loss.

Druux
  • 143
  • 1
  • 2
  • 7
  • 1
    *`discoveryDoc' URL in the initialization function on https://www.googleapis.com/discovery/v1/apis/script/v1/rest?version=v1 - here a similar error occurs at a later time.* Could you expound on that a bit more in your question? – TheMaster Jun 28 '20 at 21:27
  • @TheMaster Sure, I have just updated the questions. – Druux Jun 29 '20 at 10:34
  • 1
    Could you try other api methods, say people api or maps api from `gapi`? – TheMaster Jun 29 '20 at 11:13
  • Already worked with sheets + docs via gapi - it works without any problems – Druux Jun 29 '20 at 11:20
  • @Druux - do not know if this is still relevant for you, but could you please expand on point #6 in the question? The error likely occursed because of (please note - not "due to") the `gapi.client.init()` call failing - can you show us the snippet? – Oleg Valter is with Ukraine Jul 11 '20 at 05:14
  • 1
    @OlegValter - sure, I have just updated my question. Hope it helps. Please reach out if you need more context. – Druux Jul 11 '20 at 10:44
  • @Druux - thank you. Let's try this: load the library with `client:auth2`. Then, get the token with `getAuthToken` from the identity toolkit and in the callback initialize the library like this: `gapi.client.init({ apiKey: apiKey, discoveryDocs: discoveryDocs, clientId: clientId, scope: scopes })`. Can you try that and update the question with the results? – Oleg Valter is with Ukraine Jul 11 '20 at 19:23

0 Answers0