[英]openstack cannot retrieve authentication token from API
I am trying to retrieve the authentication token from the API using requests library from python. 我正在尝试使用来自python的请求库从API检索身份验证令牌。 Here is the attempt I have made so far: 这是我到目前为止的尝试:
def get_token():
data = {
"auth" : {
"identity" : {
"methods" : [ "password" ],
"password": {
"user" : {
"name" : OS_USERNAME,
"domain": { "name": "Default" },
"password": OS_PASSWORD
}
}
}
}
}
r = requests.post(
OS_AUTH_URL+'/auth/tokens',
headers = HEADERS,
json = data, # https://stackoverflow.com/questions/9733638
verify = False
)
print(r.content)
j = json.loads(r.content)
return j['token']['user']['id']
I get token in the response : 我在回复中得到了令牌:
{
"token": {
"issued_at": "2018-07-03T11:03:59.000000Z",
"audit_ids": [
"Fg1ywtZBQ1CkigCw70If9g"
],
"methods": [
"password"
],
"expires_at": "2018-07-03T12:03:59.000000Z",
"user": {
"password_expires_at": null,
"domain": {
"id": "default",
"name": "Default"
},
"id": "e0dc5beb383a46b98dad824c5d76e719",
"name": "admin"
}
}
}
However, when I am reusing this token to get, for instance, the list of projects : 但是,当我重复使用此令牌时,例如,获取项目列表:
def get_tenantID():
r = requests.get(
OS_AUTH_URL+'/auth/projects',
headers = HEADERS,
verify = False
)
return r
r = get_token()
HEADERS['X-Auth-Project-Id'] = 'admin'
HEADERS['X-Auth-Token'] = r
r = get_tenantID()
I get this error as if I would not be authenticated: 我收到此错误,好像我不会被验证:
<Response [401]>
{"error": {"message": "The request you have made requires authentication.", "code": 401, "title": "Unauthorized"}}
Googling around and using openstack token issue
command showed me that usually, token are more like: 谷歌搜索并使用openstack token issue
命令告诉我通常,令牌更像是:
gAAAAABaCo1F1CIMVlsTBeuqYH8tm2qR29tbkmUL4vZuhCNPXJI39TQ2YzL6Twoj8fNcAyLe3WhCYW2O1YpbBF0G8mo4bt7Kf0IRsoDOoJ6uWa3RYyJ5SQNoB_5n8EnVXbKPxFYOZ_iFBnaVtL1_XDrGbwsrlDeyy8lZTDdLsqY52pUhFR-7Uow
which is not what I get with get_token
. 这不是我用get_token
得到的。
What am I doing wrong? 我究竟做错了什么?
Many thanks! 非常感谢!
When you use the auth API directly, the token issued comes in the X-Subject-Token
header. 直接使用auth API时,发出的令牌会出现在X-Subject-Token
标头中。
Thus, to retrieve in your python example, you could access the response.headers
dict like this: 因此,要在python示例中检索,您可以访问response.headers
dict,如下所示:
token = r.headers['X-Subject-Token']
More info about authentication in the Keystone v3 docs 有关Keystone v3文档中的身份验证的更多信息
1. fetch authentication token as mentioned below:
r = requests.post(
OS_AUTH_URL+'/auth/tokens',
headers = HEADERS,
json = data,
verify = False
)
token = r.headers[X-Subject-Token]
2. Pass this token in header for further request:
{
'X-Auth-Token': token
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.