首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ecobee :文档是用于curl的,不确定如何转换为Python

Ecobee :文档是用于curl的,不确定如何转换为Python
EN

Stack Overflow用户
提问于 2018-03-02 20:36:08
回答 1查看 453关注 0票数 1

Ecobee文档显示了访问API的一种方法:

代码语言:javascript
复制
#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代码时,它不起作用。

(我真的一点也不懂卷发。我从几个小时的互联网研究中得知的情况。)

我使用的代码:

代码语言:javascript
复制
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)

当我发送这个时,我得到:

代码语言:javascript
复制
{
  "status": {
    "code": 4,
    "message": "Serialization error. Malformed json. Check your request and parameters are valid."
  }
}

不知道我的json格式会有什么问题。任何帮助都是非常感谢的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-06 15:51:52

您将需要URL-转义参数中的特殊字符。

用手做这件事可能很麻烦,而且容易出错。我不是Python专家,但最初的研究建议使用内置在Python的params中的request.get()选项。例如:

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49076981

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档