【问题标题】:Error 401 using Metabase API Session Token in Google Apps Script在 Google Apps 脚本中使用 Metabase API 会话令牌时出现错误 401
【发布时间】:2021-10-29 18:28:17
【问题描述】:

我在 Google Apps Script 中编写了一个脚本来捕获一些 Metabase 数据并将其写入 Google Sheet。当我使用我的凭据进行登录时,该功能起作用并且我收到了会话令牌。

但使用脚本中的令牌访问我的卡时,会显示错误 401 Unauthenticated。

var headers = {
"method": "get",
"contentType": "application/json",
"X-Metabase-Session": "22222222-cccc-4444-9999-333333333333",
"muteHttpExceptions": true
};

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

我使用 curl 命令在本地终端中测试了 url 和会话令牌,它工作正常。 Google Apps 脚本几乎已获得授权,不会出现任何错误。有人知道吗?

【问题讨论】:

    标签: google-apps-script http-headers urlfetch metabase


    【解决方案1】:

    看到Example GET request的官方文档,可以看到curl命令示例如下。

    curl -X GET \
      -H "Content-Type: application/json" \
      -H "X-Metabase-Session: 38f4939c-ad7f-4cbe-ae54-30946daf8593" \
      http://localhost:3000/api/user/current
    

    从这个 curl 命令看来,X-Metabase-Session 似乎需要包含在请求标头中。所以当你的脚本被修改后,就变成了如下。

    修改脚本:

    var options = {
      "method": "get",
      "contentType": "application/json",
      "headers": { "X-Metabase-Session": "22222222-cccc-4444-9999-333333333333" }, // Modified
      "muteHttpExceptions": true
    };
    var response = UrlFetchApp.fetch(url, options);
    

    注意:

    • 来自I test the url and session token in my local terminal using curl command and it works normally.,在这种情况下,我认为当你测试上面修改的脚本时,它会起作用。

    参考:

    【讨论】:

    • 感谢您的回答。我之前尝试过这种格式,并且显示了相同的错误。为了避免更多的头痛,我现在使用 Python 编写了脚本,并且效果很好。当我有更多时间时,我想更好地调试这些案例。
    • @Domarys Corrêa 感谢您的回复。我带来的不便表示歉意。关于I tried this format previously and the same error was shown.,我提议的修改脚本与示例 curl 的请求相同。我为此道歉。那么关于now I made the script using Python and it's work very well.,您可以将您的python 脚本添加到您的问题中吗?通过这个,我想确认示例 curl 和您的 python 脚本之间的区别。如果您能合作解决您的问题,我很高兴。可以配合吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    • 1970-01-01
    • 2014-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多