I have a project where I am using scripts to do some calculations and manipulation of data on form submit. Every now and then the script will fail with the following error: Authorization is required to perform that action.
I have been unable to find information about this error in the support docs.
2 Questions:
To answer your questions:
1. How long does my script stay authenticated?
Depending on the publishing status of your GCP project associated with the script, the authentication will last for different periods of time.
For example, if the publishing status is set to Testing, the authentication is set to last for 7 days as a security feature.
Authorizations by a test user will expire seven days from the time of consent. If your OAuth client requests an offline access type and receives a refresh token, that token will also expire.
Moreover, some other things to take into account regarding deauthorization and the reason behind the error you are receiving:
The user has revoked your app's access.
The refresh token has not been used for six months.
The user changed passwords and the refresh token contains Gmail scopes.
The user account has exceeded a maximum number of granted (live) refresh tokens.
As pointed out in this issue on Public Issue Tracker, a possible solution is to set explicit scopes in the manifest of the Apps Script project. Depending on your use-case, you will have to adjust the ones presented below:
{
...
"oauthScopes": [
"https://www.googleapis.com/auth/spreadsheets.readonly",
"https://www.googleapis.com/auth/admin.directory.user.readonly"
],
...
2. Is there a way to automatically re-authenticate or do I just have to manually re-authenticate every time this happens?
Unfortunately, you will have to reauthorize the application manually each time.
Setting Up Your OAuth Consent Screen;
Using OAuth 2.0 to Access Google APIs;
Public Issue Tracker Issue;
Apps Script Authorization Scopes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With