首页
通过 cURL 访问Openstack各服务

通过curl 获取token

V3 接口

  1. 创建一个json文件,作为HTTP消息的内容:
$ cat token-request.json
 {
    "auth": {
        "identity": {
            "methods": [
                "password"
            ],
            "password": {
                "user": {
                        "domain":{
                                "name":"Default"
                        },
                        "name": "admin",
                        "password": "aaaaaa"
                }
            }
        },
        "scope": {
            "project": {
                "domain":{
                        "name":"Default"
                },
                "name": "admin"
            }
        }
    }
 }

  1. 按照具体情况修改上面的json文件,向keystone请求token。-si是返回消息头。token信息位于消息头的X-Subject-Token字段:
[root@controller3 xuanll]# curl -si  -d @token-request.json -H "Content-type: application/json" http://172.159.3.4:35357/v3/auth/tokens
HTTP/1.1 201 Created
Date: Thu, 12 Oct 2017 05:59:31 GMT
Server: Apache/2.4.6 (CentOS) mod_wsgi/3.4 Python/2.7.5
X-Subject-Token: gAAAAABZ3wTDWY
Vary: X-Auth-Token
x-openstack-request-id: req-f667b905-1b08-4dce-9843-bc015b4cb894
Content-Length: 8153
Connection: close
Content-Type: application/json

  1. 从上面可以看到,X-Subject-Token:gAAAAABZ3wTDWY,我们将其存放在USER_TOKEN的环境变量中:
$ export USER_TOKEN=gAAAAABZ3wTDWY

V2 接口

  1. 向keystone请求token。token信息位于消息体的access.token.id字段:
curl -d '{"auth": {"tenantName": "admin", "passwordCredentials":{"username": "admin", "password": "aaaaaa"}}}' -H "Content-type: application/json" http://172.159.3.4:5000/v2.0/tokens | python
{
    "access": {
        "metadata": {},
        "serviceCatalog": [],
        "token": {
            "audit_ids": [
                "XZ7x8ojmTteVTFPOmRu_Jw"
            ],
            "expires": "2017-10-13T06:22:54Z",
            "id": "127493c275ba4da480bf166a0f3b610f",
            "issued_at": "2017-10-13T02:22:54.243782",
            "tenant": {
                "description": "Admin Project",
                "enabled": true,
                "id": "253a9e23972f4e7ba6fa8a17249b8053",
                "name": "admin"
            }
        },
        "user": {}
    }
}
  1. 从上面可以看到,"id": "127493c275ba4da480bf166a0f3b610f",我们将其存放在USER_TOKEN的环境变量中:
$ export USER_TOKEN=127493c275ba4da480bf166a0f3b610f

通过 curl 调用接口


[root@controller3 xuanll]# curl  -H "X-Auth-Token:$USER_TOKEN"  -H "Content-type: application/json" http://172.159.3.4:35357/v3/users/8a78fef188dd406684f68804763a141a | python -mjson.tool         
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   325  100   325    0     0   1126      0 --:--:-- --:--:-- --:--:--  1124
{
    "user": {
        "default_project_id": "5650d6967a08444895a0d0105c711a43",
        "description": "aaaa",
        "domain_id": "default",
        "enabled": true,
        "id": "8a78fef188dd406684f68804763a141a",
        "links": {
            "self": "http://172.159.3.4:35357/v3/users/8a78fef188dd406684f68804763a141a"
        },
        "name": "xuanll",
        "options": {},
        "password_expires_at": null
    }
}