python3实现阿里云发短信
一、准备工作
1、安装阿里云SDK
pip install aliyun-python-sdk-core-v3
2、新建签名并等待审核通过
审核通过后,记下**【签名名称】(参数1)**,程序中会用到。
3、新建短信模板并等待审核通过
4、获取ACCESS_KEY_ID
1)进入RAM访问控制
点击左上角的菜单栏(三个横线的图标);【产品与服务】-【监控管理】-【访问控制】
2)新建用户
【登录名称】、【显示名称】按规则输入即可,勾选上【编程访问】,点击确定
再次点击左侧菜单栏【用户】,页面将刷新
点击对应的【用户登录名称/显示名称】,进入后可创建Access Key,并添加授权(需要的两项授权如下图所示),记下【ACCESS_KEY_ID】(参数3)、【ACCESS_KEY_SECRET】(参数4),等会会用到。
注:一定要授权
二、代码部分
#!/usr/bin/env python # coding=utf-8 import uuid from aliyunsdkcore.client import AcsClient from aliyunsdkcore.profile import region_provider from aliyunsdkcore.request import RpcRequest class SendSmsRequest(RpcRequest): def __init__(self): RpcRequest.__init__(self, 'Dysmsapi', '2017-05-25', 'SendSms') def get_TemplateCode(self): return self.get_query_params().get('TemplateCode') def set_TemplateCode(self, TemplateCode): self.add_query_param('TemplateCode', TemplateCode) def get_PhoneNumbers(self): return self.get_query_params().get('PhoneNumbers') def set_PhoneNumbers(self, PhoneNumbers): self.add_query_param('PhoneNumbers', PhoneNumbers) def get_SignName(self): return self.get_query_params().get('SignName') def set_SignName(self, SignName): self.add_query_param('SignName', SignName) def get_ResourceOwnerAccount(self): return self.get_query_params().get('ResourceOwnerAccount') def set_ResourceOwnerAccount(self, ResourceOwnerAccount): self.add_query_param('ResourceOwnerAccount', ResourceOwnerAccount) def get_TemplateParam(self): return self.get_query_params().get('TemplateParam') def set_TemplateParam(self, TemplateParam): self.add_query_param('TemplateParam', TemplateParam) def get_ResourceOwnerId(self): return self.get_query_params().get('ResourceOwnerId') def set_ResourceOwnerId(self, ResourceOwnerId): self.add_query_param('ResourceOwnerId', ResourceOwnerId) def get_OwnerId(self): return self.get_query_params().get('OwnerId') def set_OwnerId(self, OwnerId): self.add_query_param('OwnerId', OwnerId) def get_SmsUpExtendCode(self): return self.get_query_params().get('SmsUpExtendCode') def set_SmsUpExtendCode(self, SmsUpExtendCode): self.add_query_param('SmsUpExtendCode', SmsUpExtendCode) def get_OutId(self): return self.get_query_params().get('OutId') def set_OutId(self, OutId): self.add_query_param('OutId', OutId) # 注意:不要更改 REGION = "cn-hangzhou" PRODUCT_NAME = "Dysmsapi" DOMAIN = "dysmsapi.aliyuncs.com" # 注意:更改为自己的参数,参数从上面的教程找 sign_name = "短信签名" # 参数1 template_code = "模板CODE" # 参数2 ACCESS_KEY_ID = "ACCESS_KEY_ID" # 参数3 ACCESS_KEY_SECRET = "ACCESS_KEY_ID" # 参数4 acs_client = AcsClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET, REGION) region_provider.add_endpoint(PRODUCT_NAME, REGION, DOMAIN) def send_sms(phone_numbers, template_param=None): business_id = uuid.uuid4() sms_request = SendSmsRequest() # 申请的短信模板编码,必填 sms_request.set_TemplateCode(template_code) # 短信模板变量参数 if template_param is not None: sms_request.set_TemplateParam(template_param) # 设置业务请求流水号,必填。 sms_request.set_OutId(business_id) # 短信签名 sms_request.set_SignName(sign_name) # 短信发送的号码列表,必填。 sms_request.set_PhoneNumbers(phone_numbers) # 调用短信发送接口,返回json sms_response = acs_client.do_action_with_exception(sms_request) return sms_response if __name__ == "__main__": result = send_sms("发送的手机号", "{'code':'验证码'}") print(str(result, encoding='utf-8'))
运行结果