Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Apps Script onEdit() not accessing external API?

I am attempting to use UrlFetchApp to access an external (Enjin) API and grab a JSON blob of info. Running the function by itself (basically a get function) provides the correct HTTP response. However, when calling the function from an onEdit() trigger event, the Logger doesn't log any response?

Is there a difference when making external API requests from a trigger?

Here is the function itself:

function getUserID(name) {
  var url = "URLHERE";

  // Make a POST request with a JSON payload.
  var data = {
   'jsonrpc':'2.0',
   'id': '12345',
   'params':{
   'api_key': '123'
   },
   'method': 'UserAdmin.get'
 };
 var options = {
   'method' : 'post',
   'contentType': 'application/json',
   'payload' : JSON.stringify(data)
 };

 var response = UrlFetchApp.fetch(url, options);
 Logger.log(response.getContentText());

return 1;
}
like image 909
Pejman Poh Avatar asked Feb 25 '17 02:02

Pejman Poh


1 Answers

Already explained and suggested workAround can be found here:

UrlFetchApp.fetch() simply does not work in onEdit trigger

In short, simple trigger onEdit cannot be used to call UrlFetch. Instead, use installable triggers.

Hope that helps

like image 198
Jack Brown Avatar answered Oct 23 '22 19:10

Jack Brown