1
votes

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).

1
I'm not very familiar with Cloud Functions. Is it possible to access urls like google apps scripts UrlFetchApp can? If so, then that's one way but perhaps there are other's I'm not familiar with.Cooper
@Cooper it is possible, but I didn't find a way to get the OAuth token in a simple way (in GAS we can use ScriptApp.getOAuthToken() to get the token without sending the user to an Oauth login screen)shamisen
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 from a GET or 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.Alan Wells
@AlanWells That could solve my problem. Thanks for the insight!shamisen
@AlanWells Can you post your comment as an answer so that others may see it?Hipster Cat

1 Answers

2
votes

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