一、調用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)
文檔內容是否對您有幫助?