As far as I've read google app script is supposed to show you a prompt to review permissions automatically if you have not authorized your app yet. However this is not happening and I also cannot find a way to manually trigger such a prompt that can be used within onOpen(e)
.
What I would like to achieve is my app prompting users to review permissions when they have not authorized the app yet.
What I have tried:
- Letting GAS automatically detect used scopes.
- Manually setting scopes using the
appscript.json
file. - Using
Ui.showModalDialog()
andScriptApp.getAuthorizationInfo().getAuthorizationUrl()
to manually guide the user to permissions. However, this results in the error:
Google Apps Script: Exception: You do not have permission to call Ui.showModalDialog.
Required permissions: https://www.googleapis.com/auth/script.container.ui
CodePudding user response:
Issue:
You are using a simple trigger (onOpen
). Simple triggers cannot access services that require authorization.
Solution:
Install your trigger, either manually, following these steps, or programmatically, executing this:
function installTrigger() {
ScriptApp.newTrigger("yourOnOpenFunction") // Don't call it onOpen
.forSpreadsheet(SpreadsheetApp.getActive())
.onOpen()
.create();
}
The authorization is given when installing the trigger.
Note:
The installed trigger will run under the account who installed it. Therefore, this is not a good approach if the actions made by the trigger depend on who is executing it, and I'd suggest displaying the dialog through other means, like clicking a button, or similar.