Ecobee API文档将此视为访问其API的一种方式:
#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\}\}'
我在curl中使用了该代码,它似乎工作。但是,当我尝试我认为是等效的python代码时它不起作用。
(我真的不知道卷曲。我知道我从几个小时的互联网研究中得知。)
我正在使用的代码:
import requests
headers = {"Content-Type": "text/json", "Authorization": "Bearer AUTH_TOKEN"}
response = requests.get('https://api.ecobee.com/1/thermostat?format=json&body=\{"selection":\{"selectionType":"registered","selectionMatch":"","includeRuntime":"true"\}\}', headers=headers)
print(response.text)
当我发送这个时,我得到:
{
"status": {
"code": 4,
"message": "Serialization error. Malformed json. Check your request and parameters are valid."
}
}
不确定我的json格式有什么问题。任何帮助深表感谢。
您需要对参数中的特殊字符进行URL转义。
手动执行此操作可能会很麻烦并且容易出错。我不是Python专家,但最初的研究建议使用Python的params
中内置的request.get()
选项。例如:
import requests
url = 'https://api.ecobee.com/1/thermostat'
TOKEN = 'ECOBEEAPIACCESSTOKEN'
header = {'Content-Type':'text/json', 'Authorization':'Bearer ' + TOKEN}
payload = {'json': '{"selection":{"selectionType":"registered","selectionMatch":"","includeRuntime":"true"}}'}
response = requests.get(url, params=payload, headers=header)
print(response.url)
print(response.text)