阿里云短信验证项目部署中遇到的常见报错......
第一种:{"Message":"模板变量缺少对应参数值","RequestId":"C0A1D1C5-2247-4F74-932D-D4A8F9026332","Code":"isv.TEMPLATE_MISSING_PARAMETERS"}
对于这种搞错:模板变量缺少对应参数值,我们需要到我们的阿里云的 “模板管理”里面查看,我们在设置模板是的参数,如下:
模板里面${}里面的参数是否和我们的项目里面的view视图里面的参数一致。如下这两个参数“code”要一致,否者就会报如上错误。
第二种报错:{"Message":"None invalid mobile number","RequestId":"04CC038B-E1ED-4D29-BA47-4FC616C05245","Code":"isv.MOBILE_NUMBER_ILLEGAL"}
对于这种报错:None invalid mobile number,无效的电话号码,我们可以将telephone字符串化,如下:
send_sms(business_id, str(telephone), "xxx", "xxx", params)
下面附代码:
const.py
# -*- coding: utf-8 -*- # ACCESS_KEY_ID/ACCESS_KEY_SECRET 根据实际申请的账号信息进行替换 ACCESS_KEY_ID = "your ACCESS_KEY_ID" ACCESS_KEY_SECRET = "your ACCESS_KEY_SECRET"
demo_sms_send.py
# -*- coding: utf-8 -*- import sys from aliyunsdkdysmsapi.request.v20170525 import SendSmsRequest from aliyunsdkdysmsapi.request.v20170525 import QuerySendDetailsRequest from aliyunsdkcore.client import AcsClient import uuid from aliyunsdkcore.profile import region_provider from aliyunsdkcore.http import method_type as MT from aliyunsdkcore.http import format_type as FT import const """ 短信业务调用接口示例,版本号:v20170525 Created on 2017-06-12 """ try: reload(sys) sys.setdefaultencoding(utf8) except NameError: pass except Exception as err: raise err # 注意:不要更改 REGION = "cn-hangzhou" PRODUCT_NAME = "Dysmsapi" DOMAIN = "dysmsapi.aliyuncs.com" acs_client = AcsClient(const.ACCESS_KEY_ID, const.ACCESS_KEY_SECRET, REGION) region_provider.add_endpoint(PRODUCT_NAME, REGION, DOMAIN) def send_sms(business_id, phone_numbers, sign_name, template_code, template_param=None): smsRequest = SendSmsRequest.SendSmsRequest() # 申请的短信模板编码,必填 smsRequest.set_TemplateCode(template_code) # 短信模板变量参数 if template_param is not None: smsRequest.set_TemplateParam(template_param) # 设置业务请求流水号,必填。 smsRequest.set_OutId(business_id) # 短信签名 smsRequest.set_SignName(sign_name) # 数据提交方式 # smsRequest.set_method(MT.POST) # 数据提交格式 # smsRequest.set_accept_format(FT.JSON) # 短信发送的号码列表,必填。 smsRequest.set_PhoneNumbers(phone_numbers) # 调用短信发送接口,返回json smsResponse = acs_client.do_action_with_exception(smsRequest) # TODO 业务处理 return smsResponse # if __name__ == __main__: # __business_id = uuid.uuid1() # #print(__business_id) # params = "{"code":"12345"}" # #params = u{"name":"wqb","code":"12345678","address":"bz","phone":"13000000000"} # print(send_sms(__business_id, "18616148389", "Authencation", "SMS_160860139", params)) #
"发送短信验证码视图"
# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.http import HttpResponse from django.http import JsonResponse, HttpResponse from django.utils.crypto import get_random_string from django.core.cache import cache import uuid from utils import mcache, restful from utils.dysms_python.demo_sms_send import send_sms # Create your views here. "发送短信验证码视图" def send_sms_captcha(request): telephone = request.GET.get("telephone") business_id = uuid.uuid1() captcha = get_random_string(length=4, allowed_chars=0123456789) params = "{"captcha":"" + captcha + ""}" # print(telephone) ret = send_sms(business_id, str(telephone), "模板签名", "模板编码", params) print(ret) mcache.set_key(telephone, captcha) return JsonResponse({success: True})