阿里云短信验证码提示“Message“:“模板变量缺少对应参数值“

  • 在使用阿里云生成的SDK示例代码时,出现以下错误:
    {"Message":"模板变量缺少对应参数值","RequestId":"5073B16C-7AAF-46B3-B5F5-EEE929532E5C","Code":"isv.TEMPLATE_MISSING_PARAMETERS"}
    
  • 问题分析
    在短信模板中有${code},在代码中没有给对应参数。
  • 解决方法
# !/usr/bin/env python
# coding=utf-8

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
import json

# accessKeyId,accessSecret根据自己的用户填入
client = AcsClient('', '', 'cn-hangzhou')

# 测试验证码
params = {
    "code": 1234
}

request = CommonRequest()
request.set_accept_format('json')
request.set_domain('dysmsapi.aliyuncs.com')
request.set_method('POST')
request.set_protocol_type('https')  # https | http
request.set_version('2017-05-25')
request.set_action_name('SendSms')

request.add_query_param('RegionId', "cn-hangzhou")
request.add_query_param('PhoneNumbers', "xxxxxxx")
request.add_query_param('SignName', "xxxx")
request.add_query_param('TemplateCode', "SMS_xxxx")

# ${code}的赋值,json格式
request.add_query_param('TemplateParam', json.dumps(params))

response = client.do_action_with_exception(request)
# python2:  print(response)
print(str(response, encoding='utf-8'))
  • 运行结果
{"Message":"OK","RequestId":"1F14423C-3457-40CF-9892-8BDF299CD1DD","BizId":"419711495303714487^0","Code":"OK"}

你可能感兴趣的:(python,Django网站项目)