UrlFetch 相当于 CURL -u "<username>:<password>"

UrlFetch equivalent of CURL -u "<username>:<password>"

我有使用 headerspayload 在 GAS 中调用 CURL 的经验,但我以前从未使用 -u 选项执行过 CURL 命令。根据 API 规范,我必须使用 -u 选项。我只是不知道如何将其转换为 GAS。到目前为止,这是我的代码:

function updateStatus()
{ 
  //Build header.
  var header =
  {
      'Content-Type': 'application/json', //Set content type to JSON.
  };

  //Put it all together.
  var options =
  {
      'method'     : 'get',    
      'headers'    : header     
  };

  //Make Login call to When I work.
  var responseGetPlan = UrlFetchApp.fetch('my url', options);
  var strResponseGetPlan = responseGetPlan.getContentText();
  Logger.log('Get Plan Response: ' + strResponseGetPlan); //Log response.

  var parsedData = JSON.parse(strResponseGetPlan); //Parse into JSON format.
  var strId = parsedData.id;  
  Logger.log(strId);
}

curl -u 使用基本身份验证,这是对串联的“用户名:密码”字符串的简单 base64 编码。您可以将以下内容发送为 headers.

Authorization: 'Basic ' + Utilities.base64Encode('username:password')

参考文献: