Google Apps脚本和外部API授权在标头中失败

时间:2013-04-16 00:53:33

标签: api google-apps-script harvest

试图让它发挥作用。我在争论列表后继续得到一个失踪者。 (第6行,文件"代码")解散。我已经仔细检查了我的括号,但没有用。我错过了什么吗?

我希望这是一个合理的问题。谢谢。

function myFunction() {
  var url = "https://company.harvestapp.com/people";
  var headers = {
    "Accept": "application/xml",
    "Content-Type": "application/xml",
    "Authorization": "Basic " + Utilities.base64Encode(dude@dude.com +":"+pw)
     };
  var response = UrlFetchApp.fetch(url,headers);
  var text = response.getResponseCode();
  Logger.log(text);
}

3 个答案:

答案 0 :(得分:3)

知道了。还要感谢Brian。我终于意识到你需要一个“选项”对象,你传递方法和标题。

function myFunction() {
    var url = "https://swellpath.harvestapp.com/people/";
    var user = "dude@dude.com";
    var password = "supersecurepw";

    var headers = {
        "Accept": "application/xml",
        "Content-Type": "application/xml",
        "Authorization": "Basic "+ Utilities.base64Encode(user+":"+password)
    };

    var options = {
        "method" : "get",
        "headers" : headers 
    };

    var response = UrlFetchApp.fetch(url,options);

    Logger.log(response);
}

答案 1 :(得分:0)

编辑:

尝试:

   "Authorization": "Basic " + Utilities.base64Encode("dude@dude.com:"+pw)

答案 2 :(得分:0)

可能存在附加错误,但是您的第一个问题是 你需要添加"标题"反对"选项"对象,然后执行以下操作

    var url = "https://company.harvestapp.com/people";

    var headers = { "Accept": "application/xml",
                   "Content-Type": "application/xml",
                  "Authorization": "Basic " + Utilities.base64Encode(dude@dude.com +":"+pw)
                  };

      var options ={
                    "headers" : headers
                   };
       var response = UrlFetchApp.fetch(url,options);