gpt4 book ai didi

python - 我如何在浏览器中传递 authtoken 获取请求

转载 作者:太空宇宙 更新时间:2023-11-03 16:01:08 24 4
gpt4 key购买 nike

我如何提出以下请求

$ curl -u eyJhbGciOiJIUzI1NiIsImV4cCI6MTM4NTY2OTY1NSwiaWF0IjoxMzg1NjY5MDU1fQ.eyJpZCI6MX0.XbOEFJkhjHJ5uRINh2JA1BPzXjSohKYDRT472wGOvjc:unused -i -X GET http://127.0.0.1:5000/api/resource
HTTP/1.0 200 OK
Content-Type: application/json

处理我的 python-get 请求,而不必使用我的登录信息,而只需使用我的身份验证 token ,就像我如何让它在curl 上工作一样?

我尝试了以下代码,它抛出了我未经授权的访问

response = requests.get('http://127.0.0.1:5000/api/resource', headers={'Authorization':'eyJhbGciOiJIUzI1NiIsImV4cCI6MTQ3Nzc3MTE2MiwiaWF0IjoxNDc3NzcwNTYyfQ.eyJpZCI6MX0.3IbZmxhDXxymOzXBKnv-rbMDBV_nFYaf-IWIW5YBnWE:unused'})
print response.text

最佳答案

在curl 联机帮助页中:

-u, --user <user:password>
Specify the user name and password to use for server authentication.

在请求文档中requests documentation :

from requests.auth import HTTPBasicAuth
requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))

所以以下可能有效(未经测试):

from requests.auth import HTTPBasicAuth
response = requests.get('http://127.0.0.1:5000/api/resource', auth=HTTPBasicAuth('eyJhbGciOiJIUzI1NiIsImV4cCI6MTQ3Nzc3MTE2MiwiaWF0IjoxNDc3NzcwNTYyfQ.eyJpZCI6MX0.3IbZmxhDXxymOzXBKnv-rbMDBV_nFYaf-IWIW5YBnWE','unused')
print response.text

关于python - 我如何在浏览器中传递 authtoken 获取请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40322834/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com