When Apps Script displays Authorization is required, This error fires when your script invokes a restricted OAuth service (such as SpreadsheetApp.openById, GmailApp, or UrlFetchApp) before an interactive user has authorized those OAuth scopes, inside a simple onOpen/onEdit trigger, or while signed into multiple Google accounts in the same browser profile.
Step-by-Step Resolution
- Open the script via Extensions > Apps Script, pick the target function from the toolbar dropdown, and click Run once manually to complete the Google OAuth consent prompt.
- If your code runs on edits or openings and needs external services or cross-file access, rename the function (e.g., handleEdit) and register an Installable Trigger under Triggers > Add Trigger instead of a simple onEdit(e).
- If running inside Google Sheets and the authorization modal loops or disappears, open the spreadsheet in a clean browser Incognito profile signed into only one Google account.
- Check appsscript.json (Project Settings > Show appsscript.json manifest) to confirm all required oauthScopes match every API service used in your script.
Common Edge Cases
Simple triggers (onOpen, onEdit, onSelectionChange) execute with limited permissions without asking the user to authorize, so even a previously authorized script throws this error if triggered anonymously as a simple trigger.
When This Doesn't Work
If multiple Gmail accounts are logged in sequentially, Apps Script uses the default index /u/0/ account for OAuth dialogs. Sign out of secondary profiles or create a dedicated Chrome Profile for your Workspace account.
Prevention Tips
Use installable triggers for scripts touching DriveApp, GmailApp, or external APIs, and run a manual test run whenever adding a new Google service scope.
Relevant Tools & Guides
- JSON Formatter & Validator — Format, beautify, inspect syntax errors, and minify JSON payloads.
- URL Encoder / Decoder — Encode special characters into URL-safe UTF-8 percent-encoding and decode back.
- Read Google Apps Script Automation Guide: Connect Workspace Services with Code →
- Read How to Automate Google Sheets: From Formulas to Apps Script Workflows →
Related Errors
Frequently Asked Questions
Why does a script work when clicked Run, but fail inside onEdit(e)?
Built-in simple onEdit triggers run without authorization prompts and cannot send emails, access external files, or call UrlFetchApp. Switch to an installable Edit trigger in the Triggers panel.
How do multiple Google logins cause Apps Script authorization failures?
When logged into multiple accounts in Chrome, the Apps Script iframe session often evaluates under the default /u/0/ user rather than the spreadsheet owner.
Can custom spreadsheet formulas (=MYFUNC()) show an authorization prompt?
No. In-cell custom functions never prompt for OAuth authorization and are blocked from calling services like GmailApp or DriveApp.
Where do I inspect my script scopes?
Open Project Settings inside Apps Script and enable Show "appsscript.json" manifest file in editor to inspect or pin explicit oauthScopes.