1

I tried to get the redirect url in google sheet with the cache function to avoid the error message: Service invoked for too many times in one day. However, the following code return an error message, may i know how to fix it


function getRedirects(url) {
  var params = {
    'followRedirects': false,
    'muteHttpExceptions': true
  };
  var followedUrls = [url];
  var cache = CacheService.getScriptCache();
  var properties = PropertiesService.getScriptProperties();
  try {  
    let res = cache.get(url);

    while (true) {
      var res = UrlFetchApp.fetch(url, params);
      if (res.getResponseCode() < 300 || res.getResponseCode() > 399) {
        return followedUrls;
      }
      var url = res.getHeaders()['Location'];
      followedUrls.push(url);
    }
  }
      }


May Leung
  • 7
  • 1
  • As I can see in your code there are 2 lines in the same scope level `let res = cache.get(url);` and `var res = UrlFetchApp.fetch(url, params);`. Can you please specify how this error shows up and how `getRedirects` is called (full URL)? – Jose Vasquez Aug 24 '21 at 09:17
  • The original code is like the below: https://pastebin.com/uex4nYZq However it always show the error message : Service invoked so many times on 1 day even i only use it to fetch for 200 urls so i read the following article to fix, however it comes up with error. https://stackoverflow.com/questions/62805656/exception-service-invoked-too-many-times-for-one-day-urlfetch – May Leung Aug 27 '21 at 03:04

1 Answers1

1

You are reaching Apps Script quota limit since there's a while-true

As per the documentation Quotas are set at different levels for users of consumer (such as gmail.com). Which in your case is URL Fetch calls - 20,000 / day

I'd change the while (true) line and foresee how many requests will be made beforehand. When you use var res = UrlFetchApp.fetch(url, params); a request / day is consumed so keep in mind that unless you own a Workspace Account you won't be able to change your quota limit.

Reference

Jose Vasquez
  • 1,678
  • 1
  • 6
  • 14