使用基本身份验证的Google Apps脚本中的UrlFetchApp.fetch出现意外错误 [英] Unexpected error on UrlFetchApp.fetch in Google Apps Script using basic authentication
问题描述
我在Google Apps脚本中有以下代码,该代码使用基本身份验证通过HTTP从网页上检索CSV数据并将其放入电子表格中:
I have the following code in Google Apps Script which retrieves CSV data from a webpage via HTTP using basic authentication and places it into a spreadsheet:
CSVImport.gs
function parseCSVtoSheet(sheetName, url)
{
// Credentials
var username = "myusername";
var password = "mypassword";
var header = "Basic " + Utilities.base64Encode(username + ":" + password);
// Setting the authorization header for basic HTTP authentication
var options = {
"headers": {
"Authorization": header
}
};
// Getting the ID of the sheet with the name passed as parameter
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getSheetByName(sheetName);
var sheetId = sheet.getSheetId();
// Getting the CSV data and placing it into the spreadsheet
var csvContent = UrlFetchApp.fetch(url, options).getContentText();
var resource = {requests: [{pasteData: {data: csvContent, coordinate: {sheetId: sheetId}, delimiter: ","}}]};
Sheets.Spreadsheets.batchUpdate(resource, spreadsheet.getId());
}
直到最近我一直在努力工作,我在UrlFetchApp.fetch
行上随机收到以下错误:
This has been working up until recently where randomly I get the following error on the UrlFetchApp.fetch
line:
Exception: Unexpected error: http://www.myurl.com/data/myfile.csv (line 21, file "CSVImport")
我尝试过:
- 直接在URL中而不是在Authorization标头中放置凭据(我收到了另一个错误消息,提示不允许登录信息".)
- 当我将凭据传递到headers对象中时,将凭据正确编码为base64(没有用,相同的错误).
- 完全删除身份验证(可能是我从HTTP页面收到了401响应).
我不确定还有什么可以尝试的,为什么突然间它随机崩溃了.有什么建议吗?
I'm not sure what else to try and why this randomly broke down all of a sudden. Any advice?
推荐答案
这与一个新错误有关,请参见此处
许多用户受到影响,我建议您为这个问题可以提高知名度并有望加快这一过程.
This is related to a new bug, see here
Many users are affected, I recommend you to "star" the issue to increase visibility and hopefully accelerate the process.
这篇关于使用基本身份验证的Google Apps脚本中的UrlFetchApp.fetch出现意外错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!