What to do when run Google AppScript to check url status but results recorded 403?

30 Views Asked by At

I use this function and it works for most URLs but for some URL the result in Column B shows 403. I'm not sure if it's about the user agent so I try to force to be specific user-agent. But results still 403.

function checkUrlsAndRecordResult() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("sheet1");
  var data = sheet.getRange("A2:A").getValues(); // Assuming URLs are in column B
  var resultColumn = sheet.getRange("B2:B"); // Column B to record results
  var results = [];

  // Define a specific Chrome User-Agent
  var chromeUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36";

  var headers = {
    "User-Agent": chromeUserAgent
  };

  var options = {
    "headers": headers,
    "muteHttpExceptions": true
  };

  for (var i = 0; i < data.length; i++) {
    var url = data[i][0];
    if (url) {
      try {
        var response = UrlFetchApp.fetch(url, options); // Fetch with custom headers and options
        var responseCode = response.getResponseCode();

        // Record the result in the results array
        results.push([responseCode]);

        // You can add additional checks or error handling here if needed
      } catch (e) {
        // Handle the error gracefully
        results.push(["Error"]);
      }
    } else {
      // If there's no URL in the cell, record a blank result
      results.push(['']);
    }
  }

  // Set the results in column B
  resultColumn.setValues(results);
}
0

There are 0 best solutions below