NutzCN Logo
问答 调用微信API send()方法时消息发送失败,原因为openid为空
发布于 2382天前 作者 LGBoy 1862 次浏览 复制 上一个帖子 下一个帖子
标签:
    @Override
    public WxResp send(WxOutMsg out) {
      if (out.getFromUserName() == null)
            out.setFromUserName(openid);
        String str = Wxs.asJson(out);
        if (Wxs.DEV_MODE)
            log.debug("api out msg>\n" + str);
        return call("/message/custom/send", METHOD.POST, str);
    }
 if (out.getFromUserName() == null)
        out.setFromUserName(openid);

应该改为
if (out.getToUserName() == null)
out.setToUserName(openid);
微信发送消息接口: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140547

4 回复

这里的fromUser是指公众号啊

客服接口-发消息
接口调用请求说明

http请求方式: POST
https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN
各消息类型所需的JSON数据包如下:

发送文本消息

{
"touser":"OPENID",
"msgtype":"text",
"text":
{
"content":"Hello World"
}
}

代码中json系列化代码

public static void asJson(Writer writer, WxOutMsg msg) {
        NutMap map = new NutMap();
        map.put("touser", msg.getToUserName());
        map.put("msgtype", msg.getMsgType());
        switch (WxMsgType.valueOf(msg.getMsgType())) {
        case text:
            map.put("text", new NutMap().setv("content", msg.getContent()));
            break;
        case image:
            map.put("image", new NutMap().setv("media_id", msg.getImage().getMediaId()));
            break;
        case voice:
            map.put("voice", new NutMap().setv("media_id", msg.getVoice().getMediaId()));
            break;
        case video:
            NutMap _video = new NutMap();
            _video.setv("media_id", msg.getVideo().getMediaId());
            if (msg.getVideo().getTitle() != null)
                _video.put("title", (msg.getVideo().getTitle()));
            if (msg.getVideo().getDescription() != null)
                _video.put("description", (msg.getVideo().getDescription()));
            map.put("video", _video);
            break;
        case music:
            NutMap _music = new NutMap();
            WxMusic music = msg.getMusic();
            if (music.getTitle() != null)
                _music.put("title", (music.getTitle()));
            if (music.getDescription() != null)
                _music.put("description", (music.getDescription()));
            if (music.getMusicUrl() != null)
                _music.put("musicurl", (music.getMusicUrl()));
            if (music.getHQMusicUrl() != null)
                _music.put("hqmusicurl", (music.getHQMusicUrl()));
            _music.put("thumb_media_id", (music.getThumbMediaId()));
            break;
        case news:
            NutMap _news = new NutMap();
            List<NutMap> list = new ArrayList<NutMap>();
            for (WxArticle article : msg.getArticles()) {
                NutMap item = new NutMap();
                if (article.getTitle() != null)
                    item.put("title", (article.getTitle()));
                if (article.getDescription() != null)
                    item.put("description", (article.getDescription()));
                if (article.getPicUrl() != null)
                    item.put("picurl", (article.getPicUrl()));
                if (article.getUrl() != null)
                    item.put("url", (article.getUrl()));
                list.add(item);
            }
            _news.put("articles", list);
            map.put("news", _news);
            break;
        case mpnews:
            map.put("mpnews", new NutMap().setv("media_id", msg.getMedia_id()));
            break;
        case wxcard:
            map.put("wxcard",
                    new NutMap().setv("card_id", msg.getCard().getId())
                                .setv("card_ext", msg.getCard().getExt()));
            break;
        default:
            break;
        }
        Json.toJson(writer, map);
    }

也没有序列化fromUser,微信接口也是要求openid的

报issue或者发个pull request吧

添加回复
请先登陆
回到顶部