Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Apps Script Error - Authorization is required to perform that action

I have created an Apps Script that replaces token in Google Doc. This App Script has been deployed as API Executable. I am able to run a function in the apps script editor without any authorization error. But, it fails sometimes with Authorization error when invoked from my java web application. I get the following error:

{
  "name": "replaceTokensInDoc",
  "done": true,
  "error": {
    "code": 3,
    "message": "ScriptError",
    "details": [
      {
        "@type": "type.googleapis.com/google.apps.script.v1.ExecutionError",
        "errorMessage": "Authorization is required to perform that action.",
        "errorType": "ScriptError"
      }
    ]
  }
}

I have read in multiple places that I need to just run a function in Script Editor and provide permission to solve this issue, but it hasn't helped in my case. When I run a function in the editor, it doesn't even show the authorization dialog, which means it has all the necessary permissions. It only fails sometimes. Could somebody let me know the reason for this strange behavior?

like image 607
Mithun Avatar asked Feb 07 '26 00:02

Mithun


1 Answers

The reason for this error is Google Apps Script return the Authorization error when the access token expires_in time is less than 6 minutes. The maximum time allowed for Apps Script execution is 6 minutes, so it wants to make sure that the token doesn't get expired while the Apps Script is being executed. This is a major issue in production. This should have been published in bold in the documentation.

There is an issue already created in the Google Issue tracker. Please star it if you are facing the same issue.

https://issuetracker.google.com/issues/36762863

The workaround, until the issue is resolved, is to refresh the token if the token expires in less than 360 seconds.

if (credential.getExpiresInSeconds() <= 360) {
    credential.refreshToken();
}
like image 195
Mithun Avatar answered Feb 12 '26 10:02

Mithun