微信订阅消息模板推送报错47003 data.time.value i,及解决方案

今天又是枯燥的一天,依然敲着代码。
客户有个微信消息推送的需求,找了下官方文档
微信消息推送文档
大致看了一下,需要模板ID和微信后台的小卡片参数名。
随即便敲起了代码,首先定义模板类,代码如下:

public class Template {
    private String touser;
    private String template_id;
    private String page;
    private List<TemplateParam> templateParamList;


    public String getTouser() {
        return touser;
    }

    public void setTouser(String touser) {
        this.touser = touser;
    }

    public String getTemplate_id() {
        return template_id;
    }

    public void setTemplate_id(String template_id) {
        this.template_id = template_id;
    }

    public String getPage() {
        return page;
    }

    public void setPage(String page) {
        this.page = page;
    }



    public String toJSON() {
        StringBuffer buffer = new StringBuffer();
        buffer.append("{");
        buffer.append(String.format("\"touser\":\"%s\"", this.touser)).append(",");
        buffer.append(String.format("\"template_id\":\"%s\"", this.template_id)).append(",");
        buffer.append(String.format("\"page\":\"%s\"", this.page)).append(",");
        buffer.append("\"data\":{");
        TemplateParam param = null;
        for (int i = 0; i < this.templateParamList.size(); i++) {
            param = templateParamList.get(i);
            // 判断是否追加逗号
            if (i < this.templateParamList.size() - 1){
                buffer.append(String.format("\"%s\": {\"value\":\"%s\"},", param.getKey(), param.getValue()));
            }else{
                buffer.append(String.format("\"%s\": {\"value\":\"%s\"}", param.getKey(), param.getValue()));
            }
        }
        buffer.append("}");
        buffer.append("}");
        return buffer.toString();
    }

    public List<TemplateParam> getTemplateParamList() {
        return templateParamList;
    }

    public void setTemplateParamList(List<TemplateParam> templateParamList) {
        this.templateParamList = templateParamList;
    }

定义TemplateParam类(文档中的data),代码如下

public class TemplateParam {
    private String key;
    private String value;

    public TemplateParam(String key,String value){
        this.key=key;
        this.value=value;
    }
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
    public String getKey() {
        return key;
    }
    public void setKey(String key) {
        this.key = key;
    }

接口请求方法,如下(需要的包自行查找,我这里就不发出来了):

public static String postData(String urlStr, String data, String contentType) {
        BufferedReader reader = null;
        try {
            URL url = new URL(urlStr);
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            conn.setConnectTimeout(CONNECT_TIMEOUT);
            conn.setReadTimeout(CONNECT_TIMEOUT);
            if (contentType != null) conn.setRequestProperty("content-type", contentType);
            OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), DEFAULT_ENCODING);
            if (data == null) data = "";
            writer.write(data);
            writer.flush();
            writer.close();
            reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), DEFAULT_ENCODING));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
                sb.append("\r\n");
            }
            return sb.toString();
        } catch (IOException e) {
            //logger.error("Error connecting to " + urlStr + ": " + e.getMessage());
        } finally {
            try {
                if (reader != null)
                    reader.close();
            } catch (IOException e) {

            }
        }             return null;
    }

万事俱备,开始写测试方法

Template template=new Template();
        template.setTemplate_id("你的模板ID");
        template.setTouser("需要推送给哪位靓仔的openid");
        List<TemplateParam> paras=new ArrayList<>();
        paras.add(new TemplateParam("小卡片上的参数1","values1"));
        paras.add(new TemplateParam("小卡片上的参数2","values2"));
        paras.add(new TemplateParam("小卡片上的参数3","values3"));
        paras.add(new TemplateParam("小卡片上的参数4","values3"));
        template.setTemplateParamList(paras);
        String s1 = HttpUtil.postData("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=ACCESS_TOKEN", template.toJSON());

ACCESS_TOKEN这个值请自行替换哦!
OK,代码已经完毕,开始测试!蹦~哦豁,炸了,报的什么错呢?

{"errcode":47003,"errmsg":"argument invalid! data.小卡片上的参数2.value i}

随即我去文档上面看了一下

47003	模板参数不准确,可能为空或者不满足规则,errmsg会提示具体是哪个字段出错

说我参数不准确?随即我便查看了后台的小卡面参数,认真对照了几遍发现还是报错。
淦!丢雷罗木,我直接复制行了吧!但是事与愿违,还是报错。
既然这样的话,那就是我的参数可能为空或不满足规则了。
空不至于,应该是不满足规则。但是规则是什么呢?
然后我又去耐心的看了一遍文档。看到了【订阅消息参数值内容限制说明】这个东西。
嘿嘿,媛来如此。
报错原因解析:

time.DATA	时间	24小时制时间格式(支持+年月日),支持填时间段,两个时间点之间用“~”符号连接	例如:15:01,或:201910115:01

由于我的参数是时间,但是传的是

2019101150101

正确的参数应该为

201910115:01:01

大家伙明白了吗?
如果模板参数与微信后台对的上,报错74003的话,就去检查对应的字段是否符合文档的规则。
如果实在是不想检查的话,那我说一个懒人方法

将报错提示字段中的中文都去掉

对了,结尾再多说一句,其实我那个模板代码是复制某位大佬的(小声BB)。
感谢各位靓仔收看本期【low逼程序员的日常生活】,拜了个拜!!!

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现微信小程序推送消息给公众号并获取模板消息库的模板编号,可以参照以下步骤: 1. 在微信公众平台上创建模板消息,获取模板ID。 2. 在小程序端使用 wx.request() 方法向服务器发送请求,请求服务器将消息发送给公众号。 3. 在服务器端,可以使用 Python 的 requests 库向微信服务器发送请求,获取模板消息库的模板编号。 具体的代码实现可以参考以下示例: 小程序端代码: ``` wx.request({ url: 'https://yourserver.com/send_msg', method: 'POST', data: { template_id: 'your_template_id', openid: 'user_openid', form_id: 'form_id', data: { keyword1: { value: '消息内容' }, keyword2: { value: '消息时间' } } }, success: function(res) { console.log(res.data) } }) ``` 服务器端代码: ``` import requests import json def send_template_msg(openid, form_id, template_id, data): access_token = get_access_token() # 获取 access_token url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' + access_token headers = {'Content-Type': 'application/json'} payload = { 'touser': openid, 'template_id': template_id, 'form_id': form_id, 'data': data } response = requests.post(url, data=json.dumps(payload), headers=headers) result = json.loads(response.text) if result['errcode'] == 0: return True else: return False def get_access_token(): # 获取 access_token 的代码 return access_token ``` 在上面的代码中,send_template_msg() 函数用于向微信服务器发送模板消息,其中需要传入 openid、form_id、template_id 和 data 参数。get_access_token() 函数用于获取 access_token,这里不再赘述。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值