Ecobee文档显示了访问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\}\}'我已经用过那个代码了,它看起来很管用。然而,当我尝试我认为是等效的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格式会有什么问题。任何帮助都是非常感谢的。
发布于 2018-03-06 15:51:52
您将需要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)https://stackoverflow.com/questions/49076981
复制相似问题