Google Apps脚本和外部API

时间:2022-08-22 11:19:44

If I am not mistaken, as per UrlFetch Documentation I should be able to call the New Basecamp API from within Google Apps Script. I suspect my confusion comes from a formatting GAS mistake (and the fact that I am just beginning all this). I have googled this a long while. This is what I have and the error I get back from GAS:

如果我没有记错,根据UrlFetch文档,我应该可以在Google Apps脚本中调用New Basecamp API。我怀疑我的混淆来自格式化GAS错误(以及我刚刚开始这一切的事实)。我用Google搜索了很久。这就是我所拥有的以及从GAS返回的错误:

myCode in GAS:

GAS中的myCode:

function myFunction() {
  var url = "https://basecamp.com/******/api/v1/projects.json";
  var headers = {
                 "contentType": "application/json",
                 "headers":{ "User-Agent": "MY_APP_NAME",
                            "username:password" : "user:pass"},
                 "validateHttpsCertificates" :false
                 };

var response = UrlFetchApp.fetch(url, headers);
var text = response.getResponseCode();
Logger.log(text);
}

GAS Error Message:

GAS错误消息:

Request failed for https://basecamp.com/2166446/api/v1/projects.json returned code 401.
Server response: HTTP Basic: Access denied. (line 9, file "Code")

I hope this is a reasonable question and thank you for the help.

我希望这是一个合理的问题,谢谢你的帮助。

1 个解决方案

#1


15  

The correct header for HTTP Basic Authentication, which makes use of Utilities.base64Encode, is as follows:

使用Utilities.base64Encode的HTTP基本身份验证的正确标头如下:

"headers": {
    "User-Agent": "MY_APP_NAME (App URL/your email address)",
    "Authorization": "Basic " + Utilities.base64Encode(username + ":" + password)
},

#1


15  

The correct header for HTTP Basic Authentication, which makes use of Utilities.base64Encode, is as follows:

使用Utilities.base64Encode的HTTP基本身份验证的正确标头如下:

"headers": {
    "User-Agent": "MY_APP_NAME (App URL/your email address)",
    "Authorization": "Basic " + Utilities.base64Encode(username + ":" + password)
},