Ecobee:响应仅返回部分结果
Ecobee: Response returning only partial results
使用 Google Apps 脚本尝试使用 Ecobee API 与我的恒温器通信。在下面的代码中,json
的值为 "{ "status": { "code": 0, "message": "" } }"
。有关正确、完整的答复,请参阅问题后面 curl
的结果。
出于某种原因,response
仅获取服务器返回的三个结果(page
、thermostatList
、thermostatList
中的最后一个元素(status
)和 status
)。
我错过了什么?
Google Apps脚本如下:
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": ""
}
}
我认为问题在于 UrlFetchApp
不支持使用 get 方法作为查询字符串发送负载。我假设这是 UrlFetchApp
和 poor design on the part of the Ecobee API 的正确设计。我通过自己在查询字符串中发送 JSON 来解决问题,如下所示。
var URL = 'https://api.ecobee.com/1/thermostat?body='+encodeURIComponent(JSON.stringify(payload));
使用 Google Apps 脚本尝试使用 Ecobee API 与我的恒温器通信。在下面的代码中,json
的值为 "{ "status": { "code": 0, "message": "" } }"
。有关正确、完整的答复,请参阅问题后面 curl
的结果。
出于某种原因,response
仅获取服务器返回的三个结果(page
、thermostatList
、thermostatList
中的最后一个元素(status
)和 status
)。
我错过了什么?
Google Apps脚本如下:
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": ""
}
}
我认为问题在于 UrlFetchApp
不支持使用 get 方法作为查询字符串发送负载。我假设这是 UrlFetchApp
和 poor design on the part of the Ecobee API 的正确设计。我通过自己在查询字符串中发送 JSON 来解决问题,如下所示。
var URL = 'https://api.ecobee.com/1/thermostat?body='+encodeURIComponent(JSON.stringify(payload));