一、调用AssumeRole接口获取token值
说明
无法使用主账号调用,只能使用 RAM 用户或 RAM 角色调用。
报错示例:You are not authorized to do this action. You should be authorized by RAM.
1、为RAM用户添加系统策略(AliyunSTSAssumeRoleAccess)
2、切换到RAM用户登录
说明
RAM账号创建登录密码:
用户---认证管理---修改登录设置---设置密码,自定义密码
3、调用安全令牌产品的AssumeRole接口:https://api.aliyun.com/api/Sts/2015-04-01/AssumeRole?spm=a2c4g.11186623.0.0.17d9595cpEplh1¶ms={}&tab=DEMO&lang=PYTHON
请求参数:
RoleArn:要扮演的RAM角色ARN,格式:acs:ram::<account_id>:role/<role_name>
RoleSessionName:角色会话名称。
在返回值中获取到3个Key值:
AccessKeyId
AccessKeySecret
SecurityToken
{'headers': {'date': 'Wed, 03 Jul 2024 09:40:20 GMT', 'content-type': 'application/json;charset=utf-8', 'content-length': '951', 'connection': 'keep-alive', 'keep-alive': 'timeout=25', 'access-control-allow-origin': '*', 'access-control-expose-headers': '*', 'x-acs-request-id': '11A37B5C-2CB0-5254-B971-74EA2DCBA224', 'x-acs-trace-id': '362308e551b03aa0219841caaf682969', 'etag': '9DgckwPseC/HB0XT4JpEljQ1'}, 'statusCode': 200, 'body': {'AssumedRoleUser': {'Arn': 'acs:ram::xxxxxxxuidxxxxxxx:role/dmrole/dmrole', 'AssumedRoleId': '327483xxxxxxxx5424:dmrole'}, 'Credentials': {'AccessKeyId': 'STS.NT4dM5xYVquAzxxxxxxxE', 'AccessKeySecret': 'Gud6u3TrbXRXKDBmKb6BgCk8zXckxxxxxxxxxxx', 'Expiration': '2024-07-03T10:40:20Z', 'SecurityToken': 'xxxxxxxxxxxxxx'}, 'RequestId': '11A37B5C-2CB0-5254-B971-74EA2DCBA224'}}
二、通过3个Key值调用邮件推送接口
from alibabacloud_dm20151123.client import Client as Dm20151123Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_dm20151123 import models as dm_20151123_models
from alibabacloud_tea_util import models as util_models
def create_client(access_key_id, access_key_secret, security_token):
config = open_api_models.Config(
# 必填,您的 AccessKey ID,
access_key_id=access_key_id,
# 必填,您的 AccessKey Secret,
access_key_secret=access_key_secret,
# 必填,您的 Token,
security_token=security_token
)
# 访问的域名
config.endpoint = f'dm.aliyuncs.com'
return Dm20151123Client(config)
accesskeyid = 'xxxxxxxxxxx'
accesskeysecret = 'xxxxxxxxxxx'
security_token = 'xxxxxxxxxxx'
client = create_client(accesskeyid, accesskeysecret, security_token)
single_send_mail_request = dm_20151123_models.SingleSendMailRequest(
account_name='test1@example.net',
address_type=1,
reply_to_address="True",
to_address='test2@example.net',
subject='test',
html_body='testbody'
)
runtime = util_models.RuntimeOptions()
v_response = client.single_send_mail_with_options(single_send_mail_request, runtime)
print(v_response)
文档内容是否对您有帮助?