2
votes

I built the Google Apps Script application that fetches data through external API.

I get expected result when run this app, without any errors. However, when run it by Time-Driven Trigger, I get the error. Anyone know why this error occurs?

the code:

function triggerFetchTest() {

  // get token
  try {

    var id = "my_id",
        password = "my_password",
        tokenUrl = ("https://api.example.jp/v1/foo/auth/get_token?id=" + id
                     + "&password=" + password),
        tokenResponse = UrlFetchApp.fetch(tokenUrl);

    var token = JSON.parse(tokenResponse.getContentText())["token"];

    Logger.log(token);

  }

  catch(e) {

    Logger.log(e);

  }

}

Additionally:

  • I can get correct response if I press "RUN" button in Script Editor.
  • I don't have any problem setting trigger.
  • I tried to set "muteHttpExceptions": true, but the error occurred.
  • The code below that shown in External APIs Doc is ok when even by triggered.

var url = 'https://gdata.youtube.com/feeds/api/videos?' + 'q=skateboarding+dog' + '&start-index=21' + '&max-results=10' + '&v=2'; var response = UrlFetchApp.fetch(url); Logger.log(response);


I want to set daily timer for this app and make the report automatically. Please give me some ideas.


*This post is my first post ever in Stackoverflow, and I'm not an English speaker in normal, so I'm sorry if you cannot read my English above...

3
What exactly is the error?Dmitry Kostyuk

3 Answers

0
votes

sama,
try to set your trigger by a function. First delete your trigger then add this function to your code and run it:

function setTrig(func){
  func=func||"triggerFetchTest";
  ScriptApp.newTrigger(func).timeBased().everyMinutes(5).create();
}

Something like this happened to me too can't remember exactly the explaination.

0
votes

Try making the main function a doGet() or doPost() and publishing the script as a web app with these settings: execute the app as me and let anyone, even anonymous access it. Then just create a another function that does a GET or POST request to your web app URI.

I think it works because the trigger does not run as you being logged in (something like AuthMode.NONE), it needs to be able to just do a urlFetch, which when you do on your app will invoke the doGet() or doPost() which is where all the work is done.

Example: https://gist.github.com/jbutters/bece2fffe85080fe4314

0
votes

The is an open issue related to Google Apps Script UrlFetch and Triggers. Please star it and leave comment on this open issue to force Google to do something about it.