【发布时间】:2018-06-12 04:04:12
【问题描述】:
目前正在尝试使用 python 请求 json,但我在响应时不断收到 401 错误。当我使用 curl 时,我收到了正确的 json 数据。
我使用的卷曲:
curl -H "X-Samanage-Authorization: Bearer API_TOKEN" -H 'Accept: application/vnd.samanage.v2.1+json' -H 'Content-Type: application/json' -X GET https://api.samanage.com/incidents.json
导致 401 错误的 Python:
import json
import requests
response = requests.get('https://api.samanage.com/incidents.json', headers={'X-Samanage-Authorization': 'API_TOKEN'})
print(response.status_code)
【问题讨论】:
-
你可以试试
headers={'X-Samanage-Authorization': 'Bearer API_TOKEN'} -
除了上面的注释:您使用的
curl命令与您在python代码中所做的不一样。curl -H "X-Samanage-Authorization: Bearer API_TOKEN" -X GET https://api.samanage.com/incidents.json是否返回正确的结果?
标签: python json curl python-requests