Execute Google Apps Script as anonymous only display the button "click me" - google-apps-script

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.

Related

Getting the the dev url instead of exec url in a deployed app

I have a bound script attached to google spreadsheet as an add on, and I also have a doPost() inside the bound script to act as a webhook for requests from my back end. I have deployed my app to test if my doPost() function works, and it does. The problem I am facing is that if I run my script from the spread sheet in which my app script is attached, I am getting a dev url when I call ScriptApp.getService().getUrl() . When I use this dev Url, it is not calling my doPost() function, but instead returns with the following error:
Sorry, unable to open the file at this time. Please check address and try again.
However, what I find interesting is that if I were to log the ScriptApp.getService().getUrl() inside my doPost() and make a request to the exec url from postman, it returns the correct exec url. So now, I a can only assume that because I am running my code from the spreadsheet, it is treating it as a different web app? I say this because when I call ScriptApp.getService().isEnabled() outside of the doPost() (in a different function inside the same script), it returns true. Is there a way to get the exec url when I run my app script from google spreadsheet? Do I have to do something else other than deploying from the app script editor?
Edit: To further clarify, I have not published my app script. Could this be the reason why?

Container-bound script deployed as Web App returns 404 (not found)

I created a Spreadsheet-bound script with a single function:
function doGet() {
Logger.log("test");
}
Then I deploy with: Execute as me, and Access - anyone, even anonymous.
However, going to published site fails with an error "Sorry, unable to open the file at this time." (or if accessed via curl - returns 404).
Exactly the same procedure works in standalone scripts. Is there a limitation or a gotcha about container-bound scripts?
This may be a bug. The published endpoint URL returned from a container-bound script has a different structure than a endpoint URL for a standalone script.
Container-bound:
https://script.google.com/macros/u/1/s/<script-id>/exec
Standalone:
https://script.google.com/a/<google-apps-domain>/macros/s/<script-id>/exec
The solution was to use the url structure of a standalone script and replace <script-id> for the script that I need. This seems to have worked.
Workaround
Container bound script URL have the following form:
https://script.google.com/macros/u/1/s/<script-id>/exec
Remove the u/1/ part:
https://script.google.com/macros/s/<script-id>/exec
Reference
Comment to Apps Script Wrong Web App URL From Script Editor (returns status code 404, with extra /u/1 in URL)
Explanation
At this time are two issues that looks to be related
Apps Script Wrong Web App URL From Script Editor (returns status code 404, with extra /u/1 in URL)
Container-bound script is not reachable (404) when deployed as Web App
It's worth to note, from https://developers.google.com/apps-script/guides/web,
Requirements for web apps
As script can be published as a web app if it meets these requirements:
It contains a doGet(e) or doPost(e) function.
The function returns an HTML service HtmlOutput object or a Content service
TextOutput object.
As I understand the above, there is a problem with your script: it fails to return an object. Anyway, it should return
The script completed but did not return anything.
For a one line web app, try something like the following:
function doGet(e) {
return ContentService.createTextOutput('Hello world!');
}
Regarding an explanation about why your code "works" on a stand-alone script but returns an error on a bounded-script, perhaps it's a glitch and it will be solved "by itself" soon. If it doesn't, checkout the Issue Tracker (follow the link on https://developers.google.com/apps-script/)

Correct scope for Google App Script Execution API?

I'm hoping to automate some HR work by running a Google App Script via the Execution API. Without getting too much into the details, I'd like to pass employee evaluation data as a parameter into the App Script. The script will then use this data to compile an "Employee Review" GDoc.
So far, I have ran a simple test App Script using the Execution API. For example, I can successfully run a simple function which logs a string or interacts with spreadsheets. So far so good.
But I run into problems when trying to write to a GDoc (which is unfortunately integral to my task). Here's my paired down script:
// TODO: Eventually, we'll pass these variables as arguments
var docId = "MY-DOC-ID";
// Find the team member review doc
var doc = DocumentApp.openById(docId);
// Replace placeholder text
var docBody = doc.getActiveSection();
docBody.replaceText('{{DATE}}', "Date set by App Script!!!");
doc.saveAndClose();
This script works when I press the "Run" button in the App Scripts web UI. But when I try to run via the Execution API, I get:
{
"error": "unauthorized_client",
"error_description": "Unauthorized client or scope in request."
}
So apparently I haven't provided the correct scope? Following the docs, I can find the necessary scope(s) in Project Properties > Scopes which says:
But when I try adding that scope, it wont work. As I said other scopes (e.g. https://www.googleapis.com/auth/spreadsheets) work just fine. Perhaps the auth/documents scope is no longer supported or there's a bug in their API?
Questions
What is the correct scope? I can see a big list here but I don't see https://www.googleapis.com/auth/documents, so?
Any other suggestions? For example, is it possible to write to a Google Doc using the Google Client API directly (i.e. without using App Scripts)?
Doh. I figured out the solution to my problem. While it was a dumb mistake, it's nevertheless worth posting as it may save others confusion in the future.
First, a little context about my setup. I'm authenticating to the Google Client API using a Service Account. Furthermore, as is common when using a service account setup, I am impersonating a user within our organization (specifically my own account).
My missing step (obvious in hindsight)...
Log into the App Script web UI as the person you are impersonating.
Manually run the script by pressing the play button
If the impersonated user has not already granted permissions to access the required scopes, you will be prompted to do so.
After granting access (specifically for the https://www.googleapis.com/auth/documents scope), my authorization error disappeared.
So the lesson: Make sure the account you are impersonating has granted access for all the scopes which your script requires.

Script function not found: doGet

I keep getting this error message when I try to click "Test web app for your latest code." in the Publish dialog box.
But I haven't defined any function called doGet().
My code is only:
function unreadCount() {
var unreadNum = "Messages unread in inbox: " + GmailApp.getInboxUnreadCount();
return unreadNum
}
Every webapp in Google Apps Script must have a main function called doGet() which is the entry point of the app, the function that your app will start with when you type the webapp url.
This is true for every application deployed as a standalone app and called by its url - with a user interface or not.
If you read the documentation you'll see that all the standalone apps examples for HTMLService or UiApp have a doGet function.
Only container embedded ui scripts or scripts that run on triggers are not concerned by this rule.
Knowing that, the error message you get is probably more meaningful isn't it ?
As Serge insas said, you must first implement the function doGet(e) as described in Google's documentation.
However, after you implement it, make sure to save a new version of your project (File > Manage versions... and then save the new version) and then deploy the new version of your web app. Else you will continue to get the "doGet not implemented" error from Google. This is because it is still using the old version of your application, before you implemented doGet.
I received this error because I had defined a doPost() function in my Google Script but my form was submitting a GET request rather than a POST request.
The solution to this problem was to submit a POST request from the form:
<form action='google-script-here' method='post'>

Creating time-based trigger from WebApp

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