大佬教程收集整理的这篇文章主要介绍了Python 使用 Bearer Token 获取 API 响应,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我检查了所有示例,但没有一个显示像我这样的端点。我如何使用端点访问 API,以便:
end_point = f"https://something.com/version/input1/{0}/input2/{1}".format(payload["input1"],payload["input2"])
API_token = {"authorization": "Bearer LonGTokEN"}
API_response = requests.post(url=end_point,auth=API_token['authorization'])
我收到了 TypeError: 'str' object is not callable
。基于此 solution,我需要 data
,但我不确定应该如何使用我的端点。
另外,不确定是否正确调用了不记名令牌。
请帮忙。谢谢。
好的。那是我的愚蠢。我对代码所做的更改似乎有效。
class BearerAuth(requests.auth.AuthBasE):
def __init__(self,token):
self.token = token
def __call__(self,r):
r.headers["authorization"] = "Bearer " + self.token
return r
def test_api_no_pax(mock_input,expected_output):
"""
TesTing API after deployment (no paX)
"""
with open(f"./assets/{mock_input}.json") as json_file:
payload = json.load(json_filE)
end_point = "https://something.com/version/input1/{0}/input2/{1}".format(payload["input1"],payload["input2"])
api_token = {"authorization": "LonGTokEN"} # removed Bearer since I use BearerAuth
api_response = requests.get(url=end_point,auth=BearerAuth(api_token['authorization']))
print(api_response.text)
尽管我希望无需编写 BearerAuth
即可获得更简洁的代码。强烈鼓励更好的解决方案!
编辑:
不使用 BearerAuth
类。
api_token = {"authorization": "Bearer LonGTokEN"}
api_response = requests.get(url=end_point,headers=api_token)
,
我猜问题是将 f-String
与 format
混合在一起。
您需要使用其中之一:
end_point = "https://something.com/version/input1/{0}/input2/{1}".format(payload["input1"],payload["input2"])
或:
end_point = f"https://something.com/version/input1/{payload['input1']}/input2/{payload['input2']}"
以上是大佬教程为你收集整理的Python 使用 Bearer Token 获取 API 响应全部内容,希望文章能够帮你解决Python 使用 Bearer Token 获取 API 响应所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。