Authorization Errors with Apps Script Execution API

Published in: Google Apps Script

A web form was built using the Google Apps Script Execution API and it would only be using to users who have successfully authenticated using their Gmail / Google Apps account. The form data would go into a Google Spreadsheet but, in some case, when the user would submit the form, the script would throw an error.

Authorization is required to perform that action. This is puzzling because the user has already authenticated through Google OAuth 2.0 and the error is not consistent either. The error 401 Invalid Credentials suggest that the OAuth access token you’re using with the project is either expired or invalid.

The auth token provided by Google automatically expires in one hour. Thus if a person has authenticated the form but leave it unattended for more than an hour, the token would automatically expire and the Google API would return an error saying that authorization is required.

An easy workaround would be to auto refresh the token every 45 minutes. This can be done by calling gapi.auth.authorize with the client ID, the scope and immediate:true as parameters.

// OAuth Token expires every hour,
// so refresh every 45 minutes

window.setInterval(refreshOAuthToken, 1000 * 60 * 45);

function refreshOAuthToken() {
      client_id: CLIENT_ID,
      scope: SCOPES,
      immediate: true,
    function (r) {
      console.log('OAuth Token Refreshed');

You can go to the Chrome developer’s console and use the expires_at field to know how much time is left before the token will expire.

new Date(gapi.auth.getToken().expires_at * 1000);

Call refreshOAuthToken() and the expires_at field with advance by 60 minutes.

📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Google Apps Script

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube