Ibm cloud 如何使用python为分析引擎请求生成IAM访问令牌?

Ibm cloud 如何使用python为分析引擎请求生成IAM访问令牌?,ibm-cloud,analytics-engine,Ibm Cloud,Analytics Engine,for Analytics引擎提供了使用CLI生成IAM访问令牌的链接,但我需要使用API调用生成令牌。这是CLI方法: bx api https://api.ng.bluemix.net bx login <enter your credentials> <If you are part of multiple IBM Cloud accounts, you'll be asked to choose an account for the current session.

for Analytics引擎提供了使用CLI生成IAM访问令牌的链接,但我需要使用API调用生成令牌。这是CLI方法:

bx api https://api.ng.bluemix.net
bx login
<enter your credentials>

<If you are part of multiple IBM Cloud accounts, you'll be asked to choose an account for the current session. Also, you'll need to choose an organization and space in IBM Cloud.>

bx iam oauth-tokens
bxapihttps://api.ng.bluemix.net
bx登录
bx iam oauth代币

文档还指出CloudFoundry API已被弃用?如何生成IAM访问令牌?

这是我最后创建的代码

用于日志记录和异常的一些实用程序类:

导入请求
导入json
从datetime导入datetime,timedelta
导入日志记录
导入操作系统
类别记录器:
定义初始化(自):
格式='%(asctime)s-%(名称)s-%(levelname)s-%(消息)s'
logging.basicConfig(格式=格式)
self.ch=logging.StreamHandler()
def get_记录器(自身、clazz):
logger=logging.getLogger(clazz)
setLevel(os.getenv(“LOG_LEVEL”,logging.INFO))
返回记录器
类CloudFoundryException(异常):
定义初始化(self,message,*args):
self.message=消息
super(CloudFoundryException,self)。\uuuuu init\uuuu(消息,*args)
然后一节课做主要工作:

class CloudFoundryAPI(对象):
def\uuuu init\uuuuu(self,api\u key=None,api\u key\u filename=None,api\u endpoint=None)https://api.ng.bluemix.net,规定轮询超时时间(分钟=30):
self.log=Logger()
self.provision\u poll\u timeout\u mins=provision\u poll\u timeout\u mins
断言api_key不是None或api_key_filename不是None,“必须为api_key或api_key_filename提供一个值”
#允许测试覆盖api_key_filename参数
如果hasattr(CloudFoundryAPI,“api\u key\u filename”)和CloudFoundryAPI不是None:
api_key_filename=CloudFoundryAPI.api_key_filename
如果api_key_filename不是None:
尝试:
打开(api_密钥_文件名,'r')作为api_文件:
d=json.load(api_文件)
尝试:
self.api_key=d['apikey']
除KeyError外:
#阿提布特的名字过去是
self.api_key=d['apiKey']
除:
self.log.error('从文件{}检索“apiKey”时出错。格式(api\u key\u文件名))
提升
其他:
self.api\u key=api\u key
self.api\u endpoint=api\u endpoint
self.info=self.\u get\u info()
def认证(自我):
self.log.debug('向CloudFoundry进行身份验证')
url=self.info['authorization\u endpoint']+'/oauth/token'
标题={
“内容类型”:“应用程序/x-www-form-urlencoded;字符集=utf-8”,
“接受”:“application/x-www-form-urlencoded;charset=utf-8”,
“授权”:“基本Y2Y6”
}
数据='grant_type=password&username=apikey&password={}'。格式(self.api_-key)
尝试:
response=requests.post(url,headers=headers,data=data)
响应。针对_状态()提出_
除了requests.exceptions.RequestException作为e:
self.log.error('Cloud Foundry Auth Response:'+Response.text)
#TODO我们应该为此定义自定义应用程序异常
提升
self.auth_token=response.json()
self.expires_at=datetime.now()+timedelta(秒=self.auth_令牌['expires_in']/60)
self.log.debug('已通过CloudFoundry身份验证')
def oidc_令牌(自身):
self.log.debug('正在检索IAM令牌')
url='1〕https://iam.bluemix.net/identity/token'
data=“grant_type=urn:ibm:params:oauth:grant type:apikey&apikey={}”。格式(self.api_-key)
尝试:
response=requests.post(url,data=data)
响应。针对_状态()提出_
除了requests.exceptions.RequestException作为e:
self.log.debug('IAM令牌响应:'+response.text)
提升
self.oidc_token=response.json()
self.oidc_expires_at=datetime.now()+timedelta(秒=self.oidc_令牌['expires_in']/60)
self.log.debug('检索到的IAM令牌')
返回self.oidc\u令牌
def get_auth_令牌(自身):
如果不是hasattr(self,'auth_token')或不是hasattr(self,'expires_at')或datetime.now()>self.expires_at:
self.auth()
返回self.auth_令牌
def get_oidc_令牌(自身):
如果不是hasattr(self,'oidc_token')或不是hasattr(self,'oidc_expires_at')或datetime.now()>self.oidc_expires_at:
self.oidc_令牌()
返回self.oidc\u令牌
定义请求头(自身):
auth\u token=self.get\u auth\u token()
access\u token=auth\u token['access\u token']
令牌类型=身份验证令牌['token\u type']
标题={
“接受”:“应用程序/json”,
'授权':'{}{}'。格式(令牌类型,访问令牌),
“缓存控制”:“无缓存”,
“内容类型”:“应用程序/json”
}
返回标题
def_请求(self,url,http_method='get',data=None,description='',create_auth_headers=True):
如果创建\u auth\u头:
headers=self.\u请求\u headers()
其他:
标题={}
尝试:
如果http_method==“get”:
response=requests.get(url,headers=headers)
elif http_方法=='post':
response=requests.post(url,headers=headers,data=json.dumps(data))
elif http_method==“delete”:
response=requests.delete(url,headers=headers)
响应。针对_状态()提出_
除了requests.exceptions.RequestException作为e:
self.log.error(“{}:{}:{}{}}.”格式(说明,http_方法,url,response.status_代码,response.text))
引发CloudFoundryException(message=response.text)
尝试:
self.log。