獲取Keystone token的三種方式

NO IMAGE

轉自:http://blog.csdn.net/alvine008/article/details/25107437

讓我們粗略看一下,三種從Keystone獲得token的方式。在嘗試這三種方式之前,你得確保已經裝好Keystone終端。如果還沒有裝好,可以看grizzly安裝版本的Keystone
安裝部分。

Note:這些呼叫都將請求的是Keystone v2版本。 

假定你已經安裝好,並且Keystone已經跑起來了:)。然後接下來我們首先要做的便是,發一個簡單的cURL傳送請求:

$
curl -d '{"auth": {"tenantName": "demo", "passwordCredentials": {"username": "demo", "password": "password"}}}' -H "Content-type: application/json" http://localhost:5000/v2.0/tokens

其中的tenantName 是作用域名。假使你省略了這個名字,將會獲得一個無作用域的token.

當請求傳送完畢,主機會返回一個JSON字串類似如下(你的可能不一樣):

{
    "access":
{
        "token":
{
            "issued_at":
"2014-02-10T00:40:20.909222",
            "expires":
"2014-02-11T00:40:20Z",
            "id":
"MIIDjwYJKoZIhvcNAQcCoIIDgDC",
            "tenant":
{
                "description":
null,
                "enabled":
true,
                "id":
"8cdca733159c4bf6a622b9bb25a73ad6",
                "name":
"demo"
            }
        },
        "serviceCatalog":
[],
        "user":
{
            "username":
"demo",
            "roles_links":
[],
            "id":
"d5cf3796f7c04a468b5282555110ba5d",
            "roles":
[
                {
                    "name":
"member"
                }
            ],
            "name":
"demo"
        },
        "metadata":
{
            "is_admin":
0,
            "roles":
[
                "a790ff829b0e4bc29d5ca4bbc58d48f1"
            ]
        }
    }
}

現在假定你不想繼續用python-keystoneclient,而是呼叫庫函式urllib2的Python專案中獲得作用域token,然後你可以這樣做:

from__future__importprint_function
 
importurllib2
importjson
 
 
if__name__=="__main__":
    json_payload={
        "auth":
{
            "tenantName":"demo",
            "passwordCredentials":
{
                "username":"demo",
                "password":"password"
            }
        }
    }
 
    headers={'content-type':'application/json','accept':'application/json'}
 
    request=urllib2.Request(url='http://localhost:5000/v2.0/tokens',
                              data=json.dumps(json_payload),
                              headers=headers)
 
    keystone_response=urllib2.urlopen(request)
 
    returned_data=json.loads(keystone_response.read())
 
    ifkeystone_response.getcode()==200:
        print(returned_data)
    else:
        print('Something
went wrong!'
)

 最後一種方式是呼叫Requests 庫,例子如下:

from__future__importprint_function
 
importrequests
importjson
 
 
if__name__=="__main__":
    json_payload={
        "auth":
{
            "tenantName":"demo",
            "passwordCredentials":
{
                "username":"demo",
                "password":"password"
            }
        }
    }
 
    headers={'content-type':'application/json','accept':'application/json'}
 
    response=requests.post(url='http://localhost:5000/v2.0/tokens',
                             data=json.dumps(json_payload),
                             headers=headers)
 
    ifresponse.status_code==requests.codes.ok:
        print(response.json())
    else:
        print('Something
went wrong!'
)

和urllib2的那種方式似乎區別不大,就是少了幾句程式碼,不過似乎更加清晰了。

當然最好還是用 Python Keystone Client 這種方式,比較容易上手,而且簡單明瞭。