Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cookies UrlFetchApp未连接到公共URL_Cookies_Google Apps Script_Http Status Code 302_Urlfetch - Fatal编程技术网

Cookies UrlFetchApp未连接到公共URL

Cookies UrlFetchApp未连接到公共URL,cookies,google-apps-script,http-status-code-302,urlfetch,Cookies,Google Apps Script,Http Status Code 302,Urlfetch,我正在尝试使用Actions>download>CSV下的download CSV链接从此网站提取数据 我在UrlFetchApp行中始终出现错误302,我不明白为什么。我运行带有httpexception的日志程序,得到的错误是它无法连接到服务器。该网站是公开的,无论网络如何,都可以访问,所以我很难理解到底发生了什么。手动输入时,URL似乎下载CSV,但我无法让UrlFetchApp获取它 var EquipUrl = "https://app.udot.utah.gov/apex/eom/

我正在尝试使用Actions>download>CSV下的download CSV链接从此网站提取数据

我在UrlFetchApp行中始终出现错误302,我不明白为什么。我运行带有httpexception的日志程序,得到的错误是它无法连接到服务器。该网站是公开的,无论网络如何,都可以访问,所以我很难理解到底发生了什么。手动输入时,URL似乎下载CSV,但我无法让UrlFetchApp获取它

var EquipUrl = "https://app.udot.utah.gov/apex/eom/f?p=288:300:::CSV";
var EquipContent = UrlFetchApp.fetch(EquipUrl).getContentText();
var EquipData = Utilities.parseCsv(EquipContent);
任何帮助都将不胜感激

我只是在评论中根据建议尝试了以下内容,并得到了相同的错误302结果。我还尝试将重定向设置为false,这将以一个空的数据数组继续和结束

var options = {
'followRedirects' :true
];
var EquipUrl = "https://app.udot.utah.gov/apex/eom/f?p=288:300:::CSV::::";
var EquipContent = UrlFetchApp.fetch(EquipUrl, options).getContentText();
var EquipData = Utilities.parseCsv(EquipContent);
感谢迄今为止的帮助!我在评论中说,代码在var cookies行中抛出了一个错误,但该错误已自行纠正,因此我认为这可能是您代码的一个坏副本。现在的问题是,最后一行记录器抛出了一个“无法解析文本”错误

我尝试了一些不同的选择,比如之前尝试登录,看看是否能找到任何东西。我是否可以从Stackdriver日志中获取任何有助于识别问题的信息

非常感谢校长!成功了!如果你能评论并解决你的问题解决过程,我会很高兴,这样我可以帮助你了解下一次发生这种情况时应该注意什么,但我知道这要求很多

  var url = 'https://app.udot.utah.gov/apex/eom/f?p=288:300::CSV::::';
  var response = UrlFetchApp.fetch(url, {
    muteHttpExceptions: true,
    followRedirects: true,
  });
  var cookie = response.getAllHeaders()['Set-Cookie']; 
  response = UrlFetchApp.fetch(url, {
    muteHttpExceptions: true,
    headers: {
      Cookie: cookie, //send the cookie we got as headerw
    },
  });
  Logger.log(Utilities.parseCsv(response.getContentText()));//parseCSV
这是一个完整功能的代码副本。再次感谢您在这几天内为解决此问题提供的帮助。

要点: 一些网站使用
cookies
来维护状态、会话和完成请求。由于
urlFetch
在服务器端运行,而不是在浏览器中运行,因此不会跨请求维护cookie。但是,我们可以在第一个请求中手动获取cookie,并在后续请求中发送它

代码段: 参考资料:

感谢您迄今为止的帮助。我实现了您建议的代码段,现在在var cookie上遇到了“TypeError:找不到对象中的函数getAllHeaders”line@John编辑您的问题以包含新代码,这会引发错误。
  var url = 'https://app.udot.utah.gov/apex/eom/f?p=288:300::CSV::::';
  var response = UrlFetchApp.fetch(url, {
    muteHttpExceptions: true,
    followRedirects: true,
  });
  var cookie = response.getAllHeaders()['Set-Cookie']; 
  response = UrlFetchApp.fetch(url, {
    muteHttpExceptions: true,
    headers: {
      Cookie: cookie, //send the cookie we got as headerw
    },
  });
  Logger.log(Utilities.parseCsv(response.getContentText()));//parseCSV
function fetchUrlWithCookie() {
  var url = 'xxxx'; //Your csv url
  var response = UrlFetchApp.fetch(url, {
    muteHttpExceptions: true,
    followRedirects: false,
  });
  var cookie = response.getAllHeaders()['Set-Cookie']; //Get cookie from header 
  response = UrlFetchApp.fetch(url, {
    muteHttpExceptions: true,
    headers: {
      Cookie: cookie, //send the cookie we got as header
    },
  });
  Logger.log(Utilities.parseCsv(response.getContentText()));//parseCSV
}