API 访问在curl 中有效,但 UrlFetchApp 返回 400 Bad Request

问题描述 投票:0回答:2

我正在尝试访问我的 Home Assistant API,使用curl 可以正常工作,但使用 UrlFetchApp 在 Google Apps 脚本中则不行。使用curl效果很好:

curl -X GET -H "Authorization: Bearer longFunkyCodeLikeThisiJ9.eyJpc3Mijk5fQ.0Fpw8I" -H "Content-Type: application/json" https://mydevice.duckdns.org:8123/api/states/sensor.my_special_sensor

Google Apps 脚本返回 400 错误请求错误。

var HOME_ASSISTANT_TOKEN = "longFunkyCodeLikeThisiJ9.eyJpc3Mijk5fQ.0Fpw8I"
var HOME_ASSISTANT_URL = "https://mydevice.duckdns.org:8123/api/"

function testTemp() {
  console.log(getDeviceParameters('sensor.my_special_sensor'));
}

function getDeviceParameters(id) {
  var url = HOME_ASSISTANT_URL + "states/" + id;
  Logger.log(url);
  var bearer = "Bearer " + HOME_ASSISTANT_TOKEN;
  var headers = {
    "Authorization": bearer,
    "Content-Type": 'application/json',
  };
  var params = {
    method: 'get',
    headers: headers
  };
  return UrlFetchApp.fetch(url, params ).getContentText();
}

我做错了什么?

google-apps-script curl oauth http-status-code-400 urlfetch
2个回答
1
投票

Google Apps 脚本似乎可能会阻止端口 8123。该脚本的功能是使用在端口 80 之外运行的 nabu casa url。

只需将

HOME_ASSISTANT_URL
值更改为
https://[yourspecialcode].ui.nabu.casa/api/


0
投票

是否确认 Google Apps 脚本正在阻止端口 8123?我犯了同样的错误。与邮递员但是一切都很好。

© www.soinside.com 2019 - 2024. All rights reserved.