我正在尝试订阅来 self 的粒子光子的事件流。 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 相对于 requests 在做什么。 谢谢您的帮助, JR
最佳答案
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
发出请求,这可以通过打印 url print(r3.网址)
关于python 请求相当于 curl -H,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31041994/