我如何提出以下请求
$ 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请求而不必使用我的登录信息,而只是我的身份验证令牌,就像我如何使用curl一样?
我尝试了以下代码,它引发了我unauthorized access
response = requests.get('http://127.0.0.1:5000/api/resource', headers={'Authorization':'eyJhbGciOiJIUzI1NiIsImV4cCI6MTQ3Nzc3MTE2MiwiaWF0IjoxNDc3NzcwNTYyfQ.eyJpZCI6MX0.3IbZmxhDXxymOzXBKnv-rbMDBV_nFYaf-IWIW5YBnWE:unused'})
print response.text
答案 0 :(得分:1)
在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
答案 1 :(得分:0)
看起来您正在尝试进行基本身份验证,但没有密码(unused
)
试试这个:
response = requests.get('http://127.0.0.1:5000/api/resource', auth=('eyJhbGciOiJIUzI1NiIsImV4cCI6MTM4NTY2OTY1NSwiaWF0IjoxMzg1NjY5MDU1fQ.eyJpZCI6MX0.XbOEFJkhjHJ5uRINh2JA1BPzXjSohKYDRT472wGOvjc', 'unused'))
print(response.text)