/ 前言 /
在ZStack的API體系中, 除登錄等個別幾個API外, 其它的API調用時都需要一個headers - Authorization
, 這個參數來自於登錄接口的返回參數, 我們來看下如何獲取到該值
/ API /
-
API名稱
使用賬戶身份登錄
-
請求方式
PUT
zstack/v1/accounts/login
-
curl示例
curl -H "Content-Type: application/json" \ -X PUT -d '{"logInByAccount":{"accountName":"test","password":"password"}}' \ http://localhost:8080/zstack/v1/accounts/login
-
返回示例
uuid就是header中
Authorization
的值{ "inventory": { "uuid": "5d77893e242d39ec8b9856a7b8762eab", "accountUuid": "7ab57f322e4a396b8124ed4fda810966", "expiredDate": "Nov 14, 2017 10:20:57 PM", "noSessionEvaluation": false }}
/ 代碼 /
user_name = 'admin'
user_password='password'
host = 'http://localhost:8080/'
# 登錄
def login():
# 登錄API需要對密碼進行SHA-512算法加密
sha512 = hashlib.sha512()
sha512.update(user_password)
password = sha512.hexdigest()
content = {
"logInByAccount": {
"password": password,
"accountName": user_name}
}
data = json.dumps(content)
url = host + 'zstack/v1/accounts/login'
headers = {"Content-Type": "application/json"}
response = requests.put(url, data, headers=headers)
# 這裏只是簡單的返回處理
if response:
rsp = json.loads(response.text)
if rsp:
return rsp['inventory']['uuid']