使用基本身份验证的 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:
异常:意外错误:http://www.myurl.com/data/myfile.csv(第 21 行,文件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?
推荐答案
这与一个新错误有关,请参阅此处
很多用户受到影响,建议你star"一下提高知名度并有望加快进程的问题.
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屋!