python 请求相当于 curl -H

标签 python curl python-requests sparkcore

我正在尝试订阅来 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 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.网址)

关于python 请求相当于 curl -H,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31041994/

相关文章:

php - cURL 获取带有 utf-8 BOM 的响应

python - 如何从 url 中提取附加的凭据?

python - 使用python范围生成递减的整数列表

Python - 列表问题(多个列表?)

python - 从其他文件导入模块,

python - 在数据框中查找具有日期模式的所有列

linux - 使用curl调用SOAP Web服务

Webdav 的 Curl c 示例在 Apache 服务器上使用摘要身份验证

python - JSON 请求出现类型错误

javascript - 如何从请求库中获取在浏览器中看到的准确页面