It is possible to call Google Apps Scripts API directly from a Google Cloud Function? If so, how to call the script functions without requesting an Oauth permission to get the authentication token?
My motivation is using Cloud Scheduler to invoke some functions of the Google Apps Script regularly (the time-based triggers on Google App Scripts don't have the sufficient resolution for my project).
You don't need to publish the Apps Script Web App as an API, you can publish it as a plain Web App, which doesn't need to go through the OAuth process in order to trigger either the doGet() or doPost() functions. doGet() is triggered to run from a GET request to the published URL of the Web App, and doPost() is triggered by a POST request. You'd need to publish the Web App to be accessible to anyone. If you make a POST request, and send a password in the payload request, that could be a way to verify that the request is coming from your code. The payload will be inside of the event object. The event object is typically denoted by the letter "e".
function doPost(e) {
For information about the event object see:
https://developers.google.com/apps-script/guides/web?hl=en#request_parameters
Related
I can't seem to access a Google Scripts webapp via HTTP requests when it's exposed to only my workspace domain, though the same works when I allow anyone (even anonymous) to access it (from a personal account created for testing)
I'm currently trying to use the Events API from Slack's and listen to calls via a Google Apps Script. Here's what that looks like on Slack's side:
This is the screen I'm pasting the Scripts URL into
Now, if I do this with my personal account, it works with the following code in the script because I've configured it to run as myself and allow anyone on the internet to run it, even anonymously:
function doPost(e) {
var v = JSON.parse(e.postData.contents); // Added
return ContentService.createTextOutput(v.challenge); // Added
}
This is how the webapp's URL looks - https://script.google.com/a/macros/scripbox.com/s/{scriptId}/exec
However, if I try to do the same from my Google Workspace account, I don't have the option to allow anonymous users to have access. The broadest option I have is "Anyone within {{workspace}}". How can I add authorization to my script URL so that anyone from my workspace's domain is able to access/use this webapp via http requests?
I'm currently using Apps Script to build a webpage. Is there a way to get the parameters in the URL without using doGet(e)? i.e. Can I use a getParams() function to retrieve the URL?
Answer
No, it is not possible
How Web-Apps work
When a user visits an app or a program sends the app an HTTP GET request, Apps Script runs the function doGet(e). When a program sends the app an HTTP POST request, Apps Script runs doPost(e) instead. In both cases, the e argument represents an event parameter that can contain information about any request parameters.
Ask for a feature request
Google has a tool called Issue Tracker that tracks bugs and feature requests during product development. You can open a new feature request there.
I'm attempting to make a post request from an Airtable script to Google Apps Script. I have a doPost() function set up in the Google Apps Script file, but the post request needs to be authenticated to run.
I believe I need to pass an OAuth token in the header of my request, but I'm unsure how to get this token in the first place. I've found this doc here about web apps in Google Apps Script but no luck finding how to generate an OAuth token from an external service.
I may be completely off the rails with my thinking, so if there's an easier way to make a post request and authenticate it from an external service, I'm all ears.
EDIT:
I don't really have any code at the moment. I'm using Postman to send the calls to Google to test. In Apps Script I have the following inside just to test.
doPost(e) { Logger.log("POST REQUEST") }
When I make the call from Postman, the function isn't triggered. In short, I know I need an authorization in the post request, but I don't know how or where to get it.
I am trying to create a custom Google Sheets function that pulls data from the Analytics Reporting V4 API. When I run this function from the App Scripts console, it successfully returns data.
When I run the function from within my spreadsheet, I get the following error:
API call to analyticsreporting.reports.batchGet failed with error: Request is missing required
authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication
credential. See https://developers.google.com/identity/sign-in/web/devconsole-project. (line 59).
Here is my function defined in the Apps Script Editor (again, this works fine when I press the run button from here):
And here is my error-producing spreadsheet implementation:
Is there a way that I can make this work without having to use OAuth credentials? From my understanding, the benefit of using Advanced Google Services is the ability to avoid this authentication flow, and I would like to take advantage of it.
In the documentation:
If your custom function throws the error message You do not have permission to call X service., the service requires user authorization and thus cannot be used in a custom function.
To use a service other than those listed above, create a custom menu that runs an Apps Script function instead of writing a custom function. A function that is triggered from a menu will ask the user for authorization if necessary and can consequently use all Apps Script services.
Using Apps Script Services
I want to use Execution api in app script to call another app script, but I see nowhere some examples to do that.
I've developed the app script api, but I don't know how to make the call in app script.
I need too to have an example to implement the oauth in this script to be able to access the app script api.
Someone can help me ?
Thanks
You may want to check this OAuth2 for Apps Script which is a library for Google Apps Script that provides the ability to create and authorize OAuth2 tokens as well as refresh them when they expire. This library uses Apps Script's new StateTokenBuilder and /usercallback endpoint to handle the redirects.