热点
微信公众号开发接入_焦点要闻
发布时间:2023-05-26 11:53:19 文章来源:博客园


(相关资料图)

微信公众号开发准备工作

你要有一个微信公众号,一个内网穿透工具

相关网站微信公众号:https://mp.weixin.qq.com/官网文档:https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Access_Overview.html需要资料服务器配置:设置与开发-->基本配置-->服务器配置token:3-32字符,自己生成配置到服务器配置公网 IP:云服务器一般都有公网IP内网穿透工具:本地测试需要穿透,否则无法对接。花生壳、natapp 等自行百度注意事项请求URL超时,说明内网穿透有问题微信验证消息和推送消息事件接口是同一地址,验证消息是GET请求 ,事件推送消息是POST。验证成功接口需要给微信原样返回随机字符串(echostr)内容,否则配置失败响应类型(Content-Type) 一定要是 text/plan切记自己对接的系统要是有权鉴,一定要放行微信消息验证接口代码示例消息验证
public void pushGet(HttpServletRequest request, HttpServletResponse response) {    String signature = request.getParameter("signature"); // 签名    String echostr = request.getParameter("echostr"); // 随机字符串    String timestamp = request.getParameter("timestamp"); // 时间戳    String nonce = request.getParameter("nonce"); // 随机数    log.debug("signature:{}", signature);    log.debug("echostr:{}", echostr);    log.debug("timestamp:{}", timestamp);    log.debug("nonce:{}", nonce);    System.out.println("signature:" + signature);    String sha1 = getSHA1(token, timestamp, nonce);    System.out.println("sha1:" + sha1);    if (sha1.equals(signature)) {        log.debug("成功");        this.responseText(echostr, response);    }}
事件推送
public void pushPost(HttpServletRequest request, HttpServletResponse response) {    String signature = request.getParameter("signature"); // 签名    String timestamp = request.getParameter("timestamp"); // 时间戳    String nonce = request.getParameter("nonce"); // 随机数    String sha1 = getSHA1(token, timestamp, nonce);    if (sha1.equals(signature)) {        Map map = null;        try {            map = XmlUtil.parseXMLToMap(request.getInputStream());        } catch (IOException e) {            e.printStackTrace();        }        log.debug("事件消息体:{}", map);        this.responseText("", response); // 回复空串,微信服务器不会对此作任何处理    }}
完整代码
import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;import lombok.extern.slf4j.Slf4j;import org.springframework.web.bind.annotation.*;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.io.PrintWriter;import java.security.MessageDigest;import java.util.*;/** * @description: 微信配置 * @author: Mr.Fang * @create: 2023-05-26 **/@Api(tags = "微信配置")@Slf4j@RestController@RequestMapping("/wx/")public class WxController {    String token="78******23";    @ApiOperation(value = "微信 token URL 验证")    @GetMapping(value = "push")    public void pushGet(HttpServletRequest request, HttpServletResponse response) {        String signature = request.getParameter("signature"); // 签名        String echostr = request.getParameter("echostr"); // 随机字符串        String timestamp = request.getParameter("timestamp"); // 时间戳        String nonce = request.getParameter("nonce"); // 随机数        log.debug("signature:{}", signature);        log.debug("echostr:{}", echostr);        log.debug("timestamp:{}", timestamp);        log.debug("nonce:{}", nonce);        System.out.println("signature:" + signature);        String sha1 = getSHA1(token, timestamp, nonce);        System.out.println("sha1:" + sha1);        if (sha1.equals(signature)) {            log.debug("成功");            this.responseText(echostr, response);        }    }    @ApiOperation(value = "接收微信事件")    @PostMapping(value = "push")    public void pushPost(HttpServletRequest request, HttpServletResponse response) {        String signature = request.getParameter("signature"); // 签名        String timestamp = request.getParameter("timestamp"); // 时间戳        String nonce = request.getParameter("nonce"); // 随机数        String sha1 = getSHA1(token, timestamp, nonce);        if (sha1.equals(signature)) {            Map map = null;            try {                // input 流返回是 xml 格式 这里转 map 了                map = XmlUtil.parseXMLToMap(request.getInputStream());            } catch (IOException e) {                e.printStackTrace();            }            log.debug("事件消息体:{}", map);            this.responseText("", response); // 回复空串,微信服务器不会对此作任何处理        }    }    /**     * 返回响应结果     *     * @param text     响应内容     * @param response     */    public void responseText(String text, HttpServletResponse response) {        response.setCharacterEncoding("UTF-8");        response.setContentType("text/plan;charset=UTF-8");        PrintWriter writer = null;        try {            writer = response.getWriter();        } catch (IOException e) {            e.printStackTrace();        }        writer.write(text);        writer.flush();        writer.close();    }    /**     * 用SHA1算法生成安全签名     *     * @param token     票据     * @param timestamp 时间戳     * @param nonce     随机字符串     * @return 安全签名     */    public String getSHA1(String token, String timestamp, String nonce) {        try {            String[] array = new String[]{token, timestamp, nonce};            StringBuffer sb = new StringBuffer();            // 字符串排序            Arrays.sort(array);            for (int i = 0; i < 3; i++) {                sb.append(array[i]);            }            String str = sb.toString();            // SHA1签名生成            MessageDigest md = MessageDigest.getInstance("SHA-1");            md.update(str.getBytes());            byte[] digest = md.digest();            StringBuffer hexstr = new StringBuffer();            String shaHex = "";            for (int i = 0; i < digest.length; i++) {                shaHex = Integer.toHexString(digest[i] & 0xFF);                if (shaHex.length() < 2) {                    hexstr.append(0);                }                hexstr.append(shaHex);            }            return hexstr.toString();        } catch (Exception e) {            e.printStackTrace();        }        return null;    }}
响应结果消息验证
signature:207e05105427e1203e769245b3860212c0ffcc56echostr:5692172970033782203timestamp:1685068850nonce:499790541signature:207e05105427e1203e769245b3860212c0ffcc56sha1:207e05105427e1203e769245b3860212c0ffcc56成功
事件推送

打开公众号发送消息,接口就可以获取到推送事件消息内容了

{"Content":"嘻嘻嘻","CreateTime":"1685068967","ToUserName":"gh_2121212a95","FromUserName":"333333333nSg8OlaSuB0d-f8FKZo","MsgType":"text","MsgId":"24124387253374797"}
其他信息

公众号配置

内网穿透

11.jpg

微信公众号开发接入_焦点要闻

京东618运动鞋服预售攻略来袭 专柜同款低至5折、定金膨胀不止10倍

预谋邂逅by久陆txt

小米Civi 3有惊喜,潮流手机开始卷性能了,还是雷军会玩 全球热头条

头条:贵州大数据产业再添新力量 中国移动大数据(贵阳)创新研究院正式揭牌

世界今热点:“运动是治愈心灵的良药”——体育活动提升中小学生心理健康水平观察

围场政协在延安组织开展党性教育活动|今日聚焦

河北内丘:产教融合 拓宽职教学子就业渠道 全球新资讯

锌价跌至成本线 中小企业订单堪忧

大宏立:5月25日融资买入94.56万元,融资融券余额1793.27万元 世界焦点

当前要闻:强化监督确保送电“不落一户、不漏一人”

视讯!方正喵呜字体怎么写教程_方正喵呜字体

环球热讯:外汇市场最新行情走势展望:纽元/美元跌至0.6050附近的年低点

宁芙|焦点要闻

宁蒗亚种-速读

【大象密码第十二期】是庞然大物,也是“运动健将” 热闻

云南博士后人才服务发展效能显现_热点

世界微速讯:在摇晃的生活里“拄”梦前行 31岁脑瘫“博士”的求学路

世界报道:教育部部署开展“2023高考护航行动”

世界热门:曲美家居年亏又季亏 定增问询背后 并购“后遗症”何解?

热点!兔子的尾巴有什么作用说明文_兔子的尾巴有什么作用

硫化氢分子量嗅阈值_硫化氢分子量

襄阳汽车客运南站主站楼封顶

C美芯晟:5月25日融资买入1402.3万元,融资融券余额2282.38万元

新天地(301277)5月25日主力资金净卖出1729.45万元

小孩七天内如何练轻功视频 小孩七天内如何练轻功-天天观热点

焦点报道:陕西省重点产业专利导航成果发布会举行

【独家】陕西省出台18条措施支持民营企业发展

世界热议:大型原创陇剧《敦煌》29日亮相

周末或有小雨 出门记得带伞|焦点热讯

食品网络电视台 | 大众经济网 | 廊坊新闻网 | 魅力中国网 | 中国财讯网 | 中国食品网络电视台 | 新选择原创 | 生活新报 | 参考经济网 | 华夏财富网 | 手机资讯 | 百通网 | 手机电商 | 瑞克网