【问题标题】:Unable to post authorization header in python requests无法在 python 请求中发布授权标头
【发布时间】:2021-02-10 20:25:17
【问题描述】:

我一直在尝试将有效负载发布到端点,端点需要不记名令牌。 使用 python 请求,我试图将不记名令牌发布在标题中

from requests import Session

http= Session()

accessToken = getToken(url=uri,data=payload)

authorization_header_string = 'bearer ' + accessToken

requestHeaders = {"Content-type": "text/plain", "Authorization":authorization_header_string}
         

headerData={"content-type":"text/plain","authorization":requestHeaders}

resp= http.post(url=uri,data=payload,headers=headerData)

但我得到的只是 401 错误,而如果我在邮递员中使用相同的令牌,它工作正常。 我正在使用 requestbin 来检查授权仅有时显示在标题中的位置。

【问题讨论】:

  • 您能否显示实际代码(自然没有实际的令牌或 URI),以便我们确定您的授权标头的格式?
  • 尝试使用:"Bearer " + accessToken;只有字段名称对 HTTP 标头不区分大小写;值可能区分大小写,也可能不区分大小写。
  • 现在更新了代码
  • 用“Bearer” + accessToken 试过还是不行

标签: python python-3.x python-requests


【解决方案1】:

尝试以下方法:

token = "your token"

headerData={"content-type": "text/plain", "authorization": "Bearer " + token}

【讨论】:

  • 是的,我使用的格式相同,我这里表示的方式有误,对不起
猜你喜欢
  • 2018-01-15
  • 2019-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-27
  • 2020-08-16
  • 1970-01-01
  • 2019-05-29
相关资源
最近更新 更多