Python 使用访问令牌的请求无效

Python request with access token not working

当我运行使用 curl 在 API 查询下方时,我得到 json 响应

curl -X GET --header 'Accept: application/json' --header 'token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c' 'https://localhost:10010/v1/machines'

但是当我尝试使用 python 请求 运行 相同的查询时,我收到访问被拒绝错误,必须提供结果代码为 403 的授权令牌。 下面是我正在尝试的python代码

import requests

url = 'https://localhost:10010/v1/machines'
head = {'Authorization': 'access_token eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c'}

response = requests.get(url, headers=head)

print response.text

如果我在这里做错了什么,请告诉我。 谢谢。

失败的原因是因为你传错了header.

使用 cURL,您通过了

--header 'token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c'

对于请求,要遵循相同的格式,并且您必须:

head = {'token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c'}
response = requests.get(url, headers=head)