Access Google AppScript API from Chrome Extension leads to 403 error

Viewed 398

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.

0 Answers
Related