我试图订阅来自我的粒子光子的事件流。 docs建议
curl -H "Authorization: Bearer {ACCESS_TOKEN_GOES_HERE}" \
https://api.particle.io/v1/events/motion-detected
我已经尝试了
address3 ='https://api.particle.io/v1/events/motion-detected'
data = {'access_token': access_token}
r3 = requests.get(address3,params=data)
但我什么也没得到,我的意思是什么,作为回应
我希望得到如下答复:
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
答案 0 :(得分:2)
Custom headers在headers
参数
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
发出请求,这可以通过打印网址print(r3.url)
答案 1 :(得分:0)
如Alik的回复中所述,自定义标头在headers
参数中作为字典传递。在你的情况下,那将是
address3 ='https://api.particle.io/v1/events/motion-detected'
data = {'Authorization': 'Bearer ' + access_token}
r3 = requests.get(address3, headers=data)
由于这是身份验证,因此最简洁的方法是实现自定义身份验证处理程序,按照documentation中的说明设置此标头。