簡體   English   中英

導入具有基本身份驗證的 CSV 時出現“服務器不支持滿足此請求所需的功能”錯誤

[英]“The server does not support the functionality needed to fulfill this request” error when importing a CSV with basic authentication

我一直在尋找一種可以將 csv 導入具有基本身份驗證的谷歌表格的方法。 到目前為止,所有在線解決方案都已過時。

我按照這里找到的方法: https://gist.github.com/jmodjeska/b0af2372c75c903700aeca4afb1fd56f

基本上是這樣做的:

    // Helper function: generate a random number for a cache busting token 
function cacheBust() {
  return Math.floor((Math.random() * 100000) + 1);
}

// Helper function: parse the CSV response
function parseCsvResponse(csvString, ignoreHeaders) {
  var retArray = [];
  var strLines = csvString.split(/\n/g);
  startLine = ignoreHeaders ? 1 : 0;
  for (var i = startLine; i < strLines.length; i++) {
    var line = strLines[i];
    if (line != '') {
      retArray.push(line.split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/));
    }
  }
  return retArray;
}

// Main function: retrieve the CSV and poppulate the data in-place
function populateSheetWithCSV(foo, csvUrl, base64pw, ignoreHeaders, bustCache) {
  var url = cacheBust ? csvUrl .concat(cacheBust()) : csvUrl;
   var resp = UrlFetchApp.fetch(url, { headers: {'Authorization': 'Basic '.concat(base64pw) }, muteHttpExceptions:true});
  var csvContent = parseCsvResponse(resp.getContentText(), ignoreHeaders);
  return csvContent;  
}

然后我通過將其放在單元格 A1 中來調用該方法:

=populateSheetWithCSV("Any Value", "https://csv-url", "base64pw", TRUE, TRUE)

粘貼公式時我在電子表格中遇到的錯誤是: The server does not support the functionality needed to fulfill this request.</u></p><hr class="line"><h3>Apache Tomcat/8.5.11

Update2:根據用戶評論,我正在嘗試在實際的 populateSheetWithCSV 方法中對用戶名、密碼和 URL 進行硬編碼(代碼的第一部分保持不變):

    // Main function: retrieve the CSV and poppulate the data in-place
function populateSheetWithCSV(foo, ignoreHeaders) {
  var USERNAME = PropertiesService.getScriptProperties().getProperty('test');
  var PASSWORD = PropertiesService.getScriptProperties().getProperty('test');
 var url = PropertiesService.getScriptProperties().getProperty('https://csv-url-redacted');///////
  var headers = {
    "Authorization" : "Basic " + Utilities.base64Encode(USERNAME + ':' + PASSWORD)
  };

  var params = {
    "method":"GET",
    "headers":headers
  };
  var resp = UrlFetchApp.fetch(url, params);
  var csvContent = parseCsvResponse(resp.getContentText(), ignoreHeaders);
  return csvContent;  

}

我收到錯誤消息: Attribute provided with no value: url (line 33). 使用第二種方法。 第 33 行是“ var resp =...

試試下面的代碼:

function populateSheetWithCSV(foo, ignoreHeaders) {
  var USERNAME = 'YOURUSERNAME';
  var PASSWORD = 'YOURPASSWORD';
 var url = 'https://csv-url-redacted';
  var headers = {
    "Authorization" : "Basic " + Utilities.base64Encode(USERNAME + ':' + PASSWORD)
  };

  var params = {
    "method":"GET",
    "headers":headers
  };
  var resp = UrlFetchApp.fetch(url, params);
  var csvContent = parseCsvResponse(resp.getContentText(), ignoreHeaders);
  return csvContent;  

}

注釋:如果您希望使用此處腳本屬性,請記住腳本屬性是鍵值對,您必須先設置一個值才能檢索它。

樣本:

PropertiesService.getScriptProperties().setProperty('URL', 'https://csv-url-redacted');
...
var url = PropertiesService.getScriptProperties().getProperty('URL');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM