python请求相当于curl H

2024-05-15 17:45:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试订阅来自粒子光子的事件流。 docs建议

curl -H "Authorization: Bearer {ACCESS_TOKEN_GOES_HERE}" \
https://api.particle.io/v1/events/motion-detected

我试过了

^{pr2}$

但我什么也得不到,我什么意思都没有

我希望得到这样的答复:

event: motion-detected
data: {"data":"intact","ttl":"60","published_at":"2015-06-25T05:08:22.136Z","coreid":"coreid"}

event: motion-detected
data: {"data":"broken","ttl":"60","published_at":"2015-06-25T05:08:23.014Z","coreid":"coreid"}

我只是不明白curl在做什么相对于请求在做什么。 谢谢你的帮助, JR公司


Tags: eventdocsdata事件粒子curl建议at
2条回答

Custom headersheaders参数中作为字典传递

address3 ='https://api.particle.io/v1/events/motion-detected'
data = {'Authorization': 'Bearer {ACCESS_TOKEN_GOES_HERE}'}
r3 = requests.get(address3, headers=data)

params参数用于传递URL parameters。基本上,您的代码向https://api.particle.io/v1/events/motion-detected?access_token=token_goes_here发出一个请求,这可以通过打印url print(r3.url)来验证

如Alik的响应中所述,自定义头作为headers参数中的字典传递。在你的情况下,那将是

address3 ='https://api.particle.io/v1/events/motion-detected'
data = {'Authorization': 'Bearer ' + access_token}
r3 = requests.get(address3, headers=data)

由于这是身份验证,最干净的方法是实现自定义身份验证处理程序,该处理程序按照documentation中的描述设置此头。在

相关问题 更多 >