Creating time-based trigger from WebApp - google-apps-script

I am trying to create a time-based trigger from a Google Apps Script published as a WebApp.
Update: this WebApp shall be run as the user accessing it, not as the Apps Script owner.
Whenver I even try to access the ScriptApp services, I get an error in the frontend:
Authorization is required to perform that action.
Sample code to reproduce this would be:
function doGet(request) {
ScriptApp.newTrigger('test');
return ContentService.createTextOutput("OK");
};
function test() {
// noop
}
Is this something that is not possible? Is it possible to somehow automatically add a trigger that runs a method based on time with the user giving the authorization?

Under what credentials is your web app run ? As the user running the app or as yourself.
From the error, it looks like it is run under the author's credentials.
In this case, you should authorize the application explicitly before you can distribute the URL. To do so, open the script editor and choose any function and try to run the function using the little 'play' button in the script editor. You'll be asked to provide permissions after which you can use the Ui App

Related

How to prompt user of container-bound script to "review permissions"

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() and ScriptApp.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
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.
Reference:
Simple triggers: Restrictions

receive parameters from another server POST request using apps script

I have some technical question about using apps script.
A third party server is sending me parameters in POST method and the request looks like this: https://script.google.com/macros/s/AKfycbyJYVLO46T1LnQKktaMrROclCOqgawcVfZaRbm_oXfJaMIYcPj8/exec?value=$postback_params_test$ (so I need to receive $postback_params_test$ as value)
I used doPost(e) function but with no success, I thing the problem is because apps script is based on client java script and it c'ant talk with server language, am I right? or there is an option to do it anyway through apps script?
my code:
function doPost(e){
var param = e.parameter.value;
var doc = SpreadsheetApp.openById("106jpepwZZWXtpO4Id45qmJovV68q_DIqpEmTQ0khf4E");
var cell = doc.getRange('a1');
cell.setValue(param);
}
8.6
Image added:
enter image description here
When deployed as a web app with settings "execute as: me" and "who has access to the app: anyone, even anonymous" your example code works.
Did you authorize the code? Before you can run it as a web app, you must run doPost() (or another function) once manually from within the script editor, so you can grant the appropriate permissions to the script.
If it's not the authorization issue, you can add a MailApp.sendemail() call to help you troubleshoot.
function doPost(e) {
MailApp.sendEmail('YOUR-EMAIL HERE','TEST doPost()',JSON.stringify(e));
This way you'll receive an email showing the raw request coming from the other server.
Be sure to re-run the script manually after adding the MailApp line so you can authorize it to send email, and update the published version.

gas spreadsheet authorization

I'm new to goole apps script.
I have created a simple test script and I would like to open a spreadsheet and read some information.
My script is authorised, I published it with:
Execute the app as: me
Who has access to the app: Anyone, even anonymous
I can open the spreadsheet manually from drive so I have access to it.
But when I run the script it shows me: "Authorisation is required to perform that action."
Here is the script:
function doGet(e) {
var doc = SpreadsheetApp.openById("0AvQ4qcr20AT2dHBDRm5QYzFqTkdyNk9jU3hRUDFoakE");
return ContentService.createTextOutput( doc.getName());
}
Do I need to make some additional authorisation using oauth or something like this ?
If so I would appreciate if I get any example.
You have to run the doGet function - or any other function in the same project - just once from the script editor before you can use the webapp.
This will trigger the authorization process (showing one or more popups depending on the services you use in that project) and is a necessary step.

Deploying and Using a Google App Script

I followed the app script guide to create a menu for my spreadsheet.
I was tested during development in the spreadsheet running it by going to:
Tools -> Script manager...
But now I want to permanently add it to the spreadsheet, but I don't know hot to deploy it.
I deployed it as a webapp, but when I click the test link i get:
Script function not found: doGet
I can not find documentation to complete that doGet function.
Any help on the last step to use my first Google App Script?
if the function that create your menu is called onOpen(e) it should automatically do the job for you when the document is opened, you don't need to publish the app or anything like that.
But in order to be executed the onOpen function must not call personnal data for the user this function onOpen (when triggered automatically, when you open the sheet) has limited rights:
in the doc https://developers.google.com/apps-script/guides/menus,
it say:
Warning: Note that the add-on menu sample above compares the
e.authMode property to values from the ScriptApp.AuthMode enum. This
is because an add-on that has not been used in the current document
runs its onOpen(e) function with severe restrictions, as explained in
the guide to the add-on authorization lifecycle. If your published
add-on fails to add its menu items, look in the browser's JavaScript
console to see if an error was thrown, then examine your script to see
whether the onOpen(e) function or global variables call services that
aren't allowed in AuthMode.NONE.

Execute Google Apps Script as anonymous only display the button "click me"

i've a good google apps script that works well, it get parameters from GET/POST for produce pdf and send it by mail and store it in google drive.
all works fine if i execute that script by my user.
i've tryed to publish to everyone, keeping execution as my user
when i try to open the generated url i see only a button "click me"
what's wrong ??
i need to call this script from GAE for execute some stuff on docs.
anyone can help me?
this is the url of my script: script
thanks
Sandro
edit
this is my test code:
function doGet() {
var output = ContentService.createTextOutput()
.setMimeType(ContentService.MimeType.TEXT)
.setContent("Hello World");
return output; }
i've publish as webapp, execution is set to my self, access is set to everyone including anonymous.
When i try to access as anonymous user, i always saw that button :(
You didn't update the version (see the menu: File > Manage Versions) and then republish the app, so you were getting the old code (aka, the sample code you get from "script as webapp"). The test URL is always the newest code, but the real URL gets an explicit version.