Alt+Enter怎么没有(enter+alt)
810
2022-05-30
遇到在某些场景下只想简单的获取华为云IAM token,然后调用一个API接口。导入huaweicloud-sdk-python包非常的重,写代码多。如果不想导入过多的包。可以通过python自带的urllibe2包或第三方包requests包,用几行代码就能获取IAM token。
如果要写一个应用需要调用多个华为云服务的API,建议导入huaweicloud-sdk-python包,参考SDK文档编写。Python SDK入门。
# _*_coding: utf-8_*_ import json import requests import urllib2 #1 Get keystone version by urllib2(GET) version_url = "https://iam.myhuaweicloud.com/v3" req = urllib2.Request(version_url) response = urllib2.urlopen(req) print("Get keystone version info:", response.read()) # IAM body data={ "auth": { "identity": { "methods": [ "password" ], "password": { "user": { "domain": { "name": "***" }, "name": "***", "password": "###" } } }, "scope": { "domain": { "name": "***" } } } } auth_url = "https://iam.myhuaweicloud.com/v3/auth/tokens?nocatalog=true" headers = """ "Content-Type": "application/json" """ json_data = json.dumps(data) #2 Get token by urllib2.Request(POST) (pyhthon native) auth_req = urllib2.Request(auth_url, json_data) auth_req.add_header('Content-Type', 'application/json') auth_resp = urllib2.urlopen(auth_req) if 'x-subject-token' in auth_resp.headers: print("Get token by urllib2.Request:", auth_resp.headers['x-subject-token']) #3 Get token by requests.post(POST) (pip install requests) auth_resp = requests.post(auth_url, json_data, headers, verify=False) if 'x-subject-token' in auth_resp.headers: print("Get token by requests.post:", auth_resp.headers['x-subject-token'])
华为云IAM
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。