【问题标题】:Google Apps Script and External APIGoogle Apps 脚本和外部 API
【发布时间】:2013-03-18 21:46:27
【问题描述】:

如果我没记错的话,按照UrlFetch Documentation,我应该可以从 Google Apps 脚本中调用New Basecamp API。我怀疑我的困惑来自格式化 GAS 错误(以及我刚刚开始这一切的事实)。我已经用谷歌搜索了很长时间。这就是我所拥有的,也是我从 GAS 得到的错误:

GAS 中的我的代码:

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 错误信息:

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

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

【问题讨论】:

    标签: api https google-apps-script basecamp


    【解决方案1】:

    使用Utilities.base64EncodeHTTP Basic Authentication 的正确标头如下:

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

    【讨论】:

    • 太棒了!谢谢!效果很好
    • @mat10112:信息到处都是。我从您原始帖子中的 API 链接开始,其中提到您可以使用 HTTP 基本身份验证。由于这比 OAuth 更容易使用,我去 Wikipedia 仔细检查了用于 HTTP 基本身份验证的确切标头。然后我查看了App Script API 文档页面,因为我知道他们有一些内置实用程序。然后我把它放在一起。
    • 好的,从现在开始我会更好地搜索。我是新手,所以当事情不那么熟悉时很难导航。再次感谢您的帮助。如果可以的话,我会投票。
    猜你喜欢
    • 2015-01-19
    • 2014-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多