【问题标题】:Convert curl command to python get request将 curl 命令转换为 python 获取请求
【发布时间】:2017-07-12 23:25:39
【问题描述】:

我订阅了一个 API,它给了我一个授权令牌和以下 curl 命令:

curl -X GET --header "Accept: application/json" --header "Authorization: Bearer 37d5a9ac5442ba9f594a8ca1421a27b" "https://blah/blah/blah"

这个 curl 在终端中工作,并在我运行它时返回一些 json。现在我需要使用返回的 json,所以我想在 JS 或 python 中执行此请求。这是我的python尝试:

url = "https://blah/blah/blah"
headers = {
    "Accept": "application/json",
    "Authentication": "Bearer 37d5a9ac5442ba9f594a8ca1421a27b"
}
response = requests.get(url, headers=headers)
print(response.text)

这会响应:

{"fault":{"code":900902,"message":"Missing Credentials","description":"Required OAuth credentials not provided. Make sure your API invocation call has a header: \"Authorization: Bearer ACCESS_TOKEN\""}}

我做错了什么?我没有像其他类似问题的答案所暗示的那样明确的用户名和密码。

【问题讨论】:

  • 执行 curl 语句导致服务器返回 200 OK?
  • @kkaosninja curl 语句打印了预期的 json,所以我假设状态码是 200。
  • @IanD 我相信 sideshowbarker 的回答应该可以解决您的问题 :-)

标签: python curl


【解决方案1】:

错误消息说您的请求需要包含一个名为Authorization 的标头。

但是您的 Python 代码 sn-p 有一个请求,其标头名称为 Authentication。

【讨论】:

    猜你喜欢
    • 2018-10-21
    • 2018-09-27
    • 2020-08-15
    • 1970-01-01
    • 2016-05-20
    • 2016-08-04
    • 2016-09-28
    • 2017-05-25
    • 2014-04-12
    相关资源
    最近更新 更多