使用Google脚本尝试使用生态蜜蜂API与我的恒温器对话。在下面的代码中,json
的值是"{ "status": { "code": 0, "message": "" } }"
。有关正确的完整响应,请参阅问题后面的curl
结果。
出于某种原因,response
只是从服务器返回的三个结果(page
、thermostatList
和status
)中获取最终元素(status
)。
我遗漏了什么?
Google脚本如下:
function getSettings() {
var response = fetchSettings();
var json = response.getContentText();
var data = JSON.parse(json);
// response, json, and data only show `status` result
}
function fetchSettings() {
try {
var headers = {
'Authorization': 'Bearer AUTH_TOKEN'
};
var payload = {
'selection' : {
'selectionType':'registered',
'selectionMatch': '',
'includeRuntime': 'true'
}
};
var options = {
'method': 'get',
'headers': headers,
'contentType': 'text/json',
'payload': JSON.stringify(payload);
};
var URL = 'https://api.ecobee.com/1/thermostat?format=json'
return UrlFetchApp.fetch(URL,options);
} catch(e) {
return e;
}
}
使用curl
的步骤如下:
curl -s -H 'Content-Type: text/json' -H 'Authorization: Bearer AUTH_TOKEN' 'https://api.ecobee.com/1/thermostat?format=json&body=\{"selection":\{"selectionType":"registered","selectionMatch":"",includeRuntime":true\}\}'
我得到了充分的结果。
{
"page": {
"page": 1,
"totalPages": 1,
"pageSize": 2,
"total": 2
},
"thermostatList": [
{
"identifier": "12345",
"name": "Downstairs",
"thermostatRev": "160528163253",
"isRegistered": true,
"modelNumber": "athenaSmart",
"brand": "ecobee",
"features": "",
"lastModified": "2016-05-28 16:32:53",
"thermostatTime": "2016-05-28 11:57:00",
"utcTime": "2016-05-28 16:57:00",
"runtime": {
"runtimeRev": "160528165546",
"connected": true,
"firstConnected": "2015-08-20 21:57:53",
"connectDateTime": "2016-05-26 08:56:18",
"disconnectDateTime": "2016-05-26 00:00:00",
"lastModified": "2016-05-28 16:55:46",
"lastStatusModified": "2016-05-28 16:55:46",
"runtimeDate": "2016-05-28",
"runtimeInterval": 200,
"actualTemperature": 703,
"actualHumidity": 49,
"desiredHeat": 670,
"desiredCool": 810,
"desiredHumidity": 44,
"desiredDehumidity": 56,
"desiredFanMode": "on"
}
},
{
"identifier": "310166836750",
"name": "Upstairs",
"thermostatRev": "160528162656",
"isRegistered": true,
"modelNumber": "athenaSmart",
"brand": "ecobee",
"features": "",
"lastModified": "2016-05-28 16:26:56",
"thermostatTime": "2016-05-28 11:57:00",
"utcTime": "2016-05-28 16:57:00",
"runtime": {
"runtimeRev": "160528165523",
"connected": true,
"firstConnected": "2015-09-28 13:33:41",
"connectDateTime": "2016-05-26 08:55:35",
"disconnectDateTime": "2016-05-26 00:00:00",
"lastModified": "2016-05-28 16:55:23",
"lastStatusModified": "2016-05-28 16:55:23",
"runtimeDate": "2016-05-28",
"runtimeInterval": 201,
"actualTemperature": 712,
"actualHumidity": 49,
"desiredHeat": 600,
"desiredCool": 840,
"desiredHumidity": 36,
"desiredDehumidity": 60,
"desiredFanMode": "auto"
}
}
],
"status": {
"code": 0,
"message": ""
}
}
发布于 2016-05-30 02:02:14
我认为问题在于,UrlFetchApp
不支持使用get方法作为查询字符串发送有效负载。我认为这是UrlFetchApp
和Ecobee部分设计不佳的正确设计。我通过自己在查询字符串中发送JSON,得到了一个kluge解决方案,如下所示。
var URL = 'https://api.ecobee.com/1/thermostat?body='+encodeURIComponent(JSON.stringify(payload));
https://stackoverflow.com/questions/37501817
复制相似问题