Spaces:
Running
Running
File size: 1,492 Bytes
5690e11 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# Copyright (c) Alibaba, Inc. and its affiliates.
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
from .exception import GetTokenFailed
import json
__all__ = ['getToken']
def getToken(akid, aksecret, domain='cn-shanghai',
version='2019-02-28',
url='nls-meta.cn-shanghai.aliyuncs.com'):
"""
Help methods to get token from aliyun by giving access id and access secret
key
Parameters:
-----------
akid: str
access id from aliyun
aksecret: str
access secret key from aliyun
domain: str:
default is cn-shanghai
version: str:
default is 2019-02-28
url: str
full url for getting token, default is
nls-meta.cn-shanghai.aliyuncs.com
"""
if akid is None or aksecret is None:
raise GetTokenFailed('No akid or aksecret')
client = AcsClient(akid, aksecret, domain)
request = CommonRequest()
request.set_method('POST')
request.set_domain(url)
request.set_version(version)
request.set_action_name('CreateToken')
response = client.do_action_with_exception(request)
response_json = json.loads(response)
if 'Token' in response_json:
token = response_json['Token']
if 'Id' in token:
return token['Id']
else:
raise GetTokenFailed(f'Missing id field in token:{token}')
else:
raise GetTokenFailed(f'Token not in response:{response_json}')
|