第一步:申请测试号

1.打开微信测试号申请平台 http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

2.点击“登录”按钮,用微信扫码确认登录

3.确认登录后,测试号申请成功!

第二步:测试号管理

1.测试号信息
这里的appID、appsecret在后面很多接口调用中都需要用到

2.接口配置信息(这里需要有自己的服务器和映射成功的域名)
(本人用的是本地node服务器,用花生壳把本机的8080端口映射到了花生壳提供的二级免费域名的80端口)

url填写自己的域名,token可以随意填

填写完后先不要提交,接下来编写nodejs服务端代码响应微信的提交请求,响应Token验证结果

server.js
*注意:以下用到的模块大部分需要先安装,安装步骤请在本人其他的博文中查看或自行百度安装

const express = require('express'); //web服务框架模块
const request = require('request'); //http请求模块
const fs = require('fs'); //文件系统模块
const path = require('path'); //文件路径模块
const sha1 = require('node-sha1'); //加密模块
const urlencode= require('urlencode'); //URL编译模块const hostName = '127.0.0.1'; //ip或域名
const port = 8080; //端口/*** [开启跨域便于接口访问]*/
app.all('*', function(req, res, next) {res.header('Access-Control-Allow-Origin', '*'); //访问控制允许来源:所有res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept'); //访问控制允许报头 X-Requested-With: xhr请求res.header('Access-Control-Allow-Metheds', 'PUT, POST, GET, DELETE, OPTIONS'); //访问控制允许方法res.header('X-Powered-By', 'nodejs'); //自定义头信息,表示服务端用nodejsres.header('Content-Type', 'application/json;charset=utf-8');next();
});/*** [设置验证微信接口配置参数]*/
const config = {token: 'test', //对应测试号接口配置信息里填的tokenappid: 'xxxxxxxxxxxxxx', //对应测试号信息里的appIDsecret: 'xxxxxxxxxxxxxxxxxxxx', //对应测试号信息里的appsecretgrant_type: 'client_credential' //默认
};/*** [验证微信接口配置信息,]*/
app.get('/', function(req, res) {const token = config.token; //获取配置的tokenconst signature = req.query.signature; //获取微信发送请求参数signatureconst nonce = req.query.nonce; //获取微信发送请求参数nonceconst timestamp = req.query.timestamp; //获取微信发送请求参数timestampconst str = [token, timestamp, nonce].sort().join(''); //排序token、timestamp、nonce后转换为组合字符串const sha = sha1(str); //加密组合字符串//如果加密组合结果等于微信的请求参数signature,验证通过if (sha === signature) {const echostr = req.query.echostr; //获取微信请求参数echostrres.send(echostr + ''); //正常返回请求参数echostr} else {res.send('验证失败');}
});app.listen(port, hostName, function() {console.log(`服务器运行在http://${hostName}:${port}`);
});

运行server.js启动node服务器,保证花生壳成功映射本地服务器至域名,外网可通过域名正常访问本地服务器

然后点击提交,提示“配置成功”表示接口配置完成

3.模板消息接口
新增测试模板,填写标题和内容

4.OAuth2.0网页授权
在网页服务 > 网页帐号 > 网页授权获取用户基本信息 > 修改

回调域名的填写自己的域名(注意,域名的格式不包含http://)

5.创建微信网页授权接口链接

在node服务器新增授权调用接口,在微信中访问这个接口地址会创建一个可以跳转到授权页面的链接

这步操作是为了使回调的接口得到code参数,因为发送模板消息必须要有用户的openid,而获取用户的openid必须要有code参数,code参数需要用户访问指定的链接触发授权后才会产生

继续在server.js的基础上增加

/*** [创建请求微信网页授权接口链接]*/app.get('/authentication', function(req, res) {const appid = config.appid;const redirect_uri = urlencode("http://www.xxx.net/code"); //这里的url需要转为加密格式,它的作用是访问微信网页鉴权接口成功后微信会回调这个地址,并把code参数带在回调地址中const scope = 'snsapi_userinfo';const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirect_uri}&response_type=code&scope=${scope}&state=STATE&connect_redirect=1#wechat_redirect`;const html =`<!DOCTYPE html><html><head><meta charset="utf-8" ><title>微信鉴权引导</title></head><body><a href="${url}">跳转到鉴权页面</a></body></html>`;res.setHeader('Content-Type', 'text/html');res.send(html);
});

*注意:每次修改server.js文件后需要重启node服务代码才会生效

6.下载安装微信开发者工具调试
安装成功后登录开发者工具,访问刚才创建的接口地址 http://www.xxx.net/authentication,
可以看到鉴权的链接已经创建成功,但目前还没有写好获取code的接口,所以先不要点击

7.获取到code > 获取openid > 获取access_token > 发送模板消息

继续在server.js的基础上增加

/*** 网页授权回调接口,可以获取code*/app.get('/code', function(req, res) {const code = req.query.code; //微信回调这个接口后会把code参数带过来getOpenId(code); //把code传入getOpenId方法});/*** 获取openid* @param  { string } code [调用获取openid的接口需要code参数]*/
function getOpenId(code) {const appid = config.appid;const secret = config.secret;const url = `https://api.weixin.qq.com/sns/oauth2/access_token?appid=${appid}&secret=${secret}&code=${code}&grant_type=authorization_code`;request(url, function(error, response, body) {if (!error && response.statusCode == 200) {const openid =  body.openid;getAccessToken(openid);   //获取openid成功后调用getAccessToken}});
}/*** 获取access_token*  @param  { string } openid [发送模板消息的接口需要用到openid参数]*/
function getAccessToken(openid) {const appid = config.appid;const secret = config.secret;const grant_type = config.grant_type;const url = `https://api.weixin.qq.com/cgi-bin/token?grant_type=${grant_type}&appid=${appid}&secret=${secret}`;request(url, function(error, response, body) {if (!error && response.statusCode == 200) {const access_token= JSON.parse(body).access_token;sendTemplateMsg(openid, access_token); //获取access_token成功后调用发送模板消息的方法} else {throw 'update access_token error';}});}/*** 发送模板消息* @param  { string } openid [发送模板消息的接口需要用到openid参数]* @param  { string } access_token [发送模板消息的接口需要用到access_token参数]*/function sendTemplateMsg(openid, access_token) {const url = `https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=${access_token}`; //发送模板消息的接口const requestData = { //发送模板消息的数据touser: openid,template_id: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',url: 'http://weixin.qq.com/download',data: {first: {value: '身份信息',color: "#173177"},keyword1: {value: '张三',color: '#1d1d1d'},keyword2: {value: '男',color: '#1d1d1d'},keyword3: {value: '45',color: '#1d1d1d'},remark: {value: '已登记!',color: '#173177'}}};request({url: url,method: 'post',body: requestData,}, function(error, response, body) {if (!error && response.statusCode == 200) {console.log('模板消息推送成功'); }});}

完整源码

server.js

const express = require('express'); //web服务框架模块
const request = require('request'); //http请求模块
const fs = require('fs'); //文件系统模块
const path = require('path'); //文件路径模块
const sha1 = require('node-sha1'); //加密模块
const urlencode= require('urlencode'); //URL编译模块/*** [设置验证微信接口配置参数]*/
const config = {token: 'test', //对应测试号接口配置信息里填的tokenappid: 'xxxxxxxxxxxxxx', //对应测试号信息里的appIDsecret: 'xxxxxxxxxxxxxxxxxxxx', //对应测试号信息里的appsecretgrant_type: 'client_credential' //默认
};/*** [开启跨域便于接口访问]*/
app.all('*', function(req, res, next) {res.header('Access-Control-Allow-Origin', '*'); //访问控制允许来源:所有res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept'); //访问控制允许报头 X-Requested-With: xhr请求res.header('Access-Control-Allow-Metheds', 'PUT, POST, GET, DELETE, OPTIONS'); //访问控制允许方法res.header('X-Powered-By', 'nodejs'); //自定义头信息,表示服务端用nodejsres.header('Content-Type', 'application/json;charset=utf-8');next();
});/*** [验证微信接口配置信息,]*/
app.get('/', function(req, res) {const token = config.token; //获取配置的tokenconst signature = req.query.signature; //获取微信发送请求参数signatureconst nonce = req.query.nonce; //获取微信发送请求参数nonceconst timestamp = req.query.timestamp; //获取微信发送请求参数timestampconst str = [token, timestamp, nonce].sort().join(''); //排序token、timestamp、nonce后转换为组合字符串const sha = sha1(str); //加密组合字符串//如果加密组合结果等于微信的请求参数signature,验证通过if (sha === signature) {const echostr = req.query.echostr; //获取微信请求参数echostrres.send(echostr + ''); //正常返回请求参数echostr} else {res.send('验证失败');}
});/*** [创建请求微信网页授权接口链接]*/
app.get('/authentication', function(req, res) {const appid = config.appid;const redirect_uri = urlencode("http://www.xxx.net/code"); //这里的url需要转为加密格式,它的作用是访问微信网页鉴权接口成功后微信会回调这个地址,并把code参数带在回调地址中const scope = 'snsapi_userinfo';const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirect_uri}&response_type=code&scope=${scope}&state=STATE&connect_redirect=1#wechat_redirect`;const html =`<!DOCTYPE html><html><head><meta charset="utf-8" ><title>微信鉴权引导</title></head><body><a href="${url}">跳转到鉴权页面</a></body></html>`;res.setHeader('Content-Type', 'text/html');res.send(html);
});/*** 网页授权回调接口,可以获取code*/
app.get('/code', function(req, res) {const code = req.query.code; //微信回调这个接口后会把code参数带过来getOpenId(code); //把code传入getOpenId方法
});/*** 获取openid* @param  { string } code [调用获取openid的接口需要code参数]*/
function getOpenId(code) {const appid = config.appid;const secret = config.secret;const url = `https://api.weixin.qq.com/sns/oauth2/access_token?appid=${appid}&secret=${secret}&code=${code}&grant_type=authorization_code`;request(url, function(error, response, body) {if (!error && response.statusCode == 200) {const openid =  body.openid;getAccessToken(openid);   //获取openid成功后调用getAccessToken}});
}/*** 获取access_token*  @param  { string } openid [发送模板消息的接口需要用到openid参数]*/
function getAccessToken(openid) {const appid = config.appid;const secret = config.secret;const grant_type = config.grant_type;const url = `https://api.weixin.qq.com/cgi-bin/token?grant_type=${grant_type}&appid=${appid}&secret=${secret}`;request(url, function(error, response, body) {if (!error && response.statusCode == 200) {const access_token= JSON.parse(body).access_token;sendTemplateMsg(openid, access_token); //获取access_token成功后调用发送模板消息的方法} else {throw 'update access_token error';}});}/*** 发送模板消息* @param  { string } openid [发送模板消息的接口需要用到openid参数]* @param  { string } access_token [发送模板消息的接口需要用到access_token参数]*/
function sendTemplateMsg(openid, access_token) {const url = `https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=${access_token}`; //发送模板消息的接口const requestData = { //发送模板消息的数据touser: openid,template_id: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',url: 'http://weixin.qq.com/download',data: {first: {value: '身份信息',color: "#173177"},keyword1: {value: '张三',color: '#1d1d1d'},keyword2: {value: '男',color: '#1d1d1d'},keyword3: {value: '45',color: '#1d1d1d'},remark: {value: '已登记!',color: '#173177'}}};request({url: url,method: 'post',body: requestData,}, function(error, response, body) {if (!error && response.statusCode == 200) {console.log('模板消息推送成功'); }});
}const hostName = '127.0.0.1'; //ip或域名
const port = 8080; //端口
app.listen(port, hostName, function() {console.log(`服务器运行在http://${hostName}:${port}`);
});

接口依赖关系图

微信网页授权文档
https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842

获取access_token文档
https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183

发送模板文档
https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277

Nodejs + express 开发微信公众号模板消息推送功能相关推荐

  1. 【Java中实现微信公众号模板消息推送】

    主要流程: 1.在微信公众测试平台上注册账号,关注测试公众号,新增消息模板 2.拿到需要的参数openId appId appsecret 模板Id后进行开发 微信公众平台测试号管理地址 https: ...

  2. Java对接微信公众号模板消息推送(架包WxJava)

    内容有点多,请耐心! 最近公司的有这个业务需求,又很凑巧让我来完成: 首先想要对接,先要一个公众号,再就是开发文档了:https://developers.weixin.qq.com/doc/offi ...

  3. Java对接微信公众号模板消息推送

    最近公司的有这个业务需求,又很凑巧让我来完成: 首先想要对接,先要一个公众号,再就是开发文档了:https://developers.weixin.qq.com/doc/offiaccount/Get ...

  4. 微信公众号模板消息推送问题汇总

    总结:经常遇到的微信模版消息推送返回失败情况! 1.{"errcode":40037,"errmsg":"invalid template_id hi ...

  5. 微信公众号模板消息推送(附上完整代码)

    官方文档 会用到的调用函数 import logging import requests import time from pickle import dumps, loadsfrom request ...

  6. 微信公众号模板消息推送(PHP)

    1.发送模板消息 public function send_notice(){$access_token = '';//模板消息$json_template = $this->json_temp ...

  7. Java实现微信公众号模板消息推送给用户

    创建消息模板实体对象 package com.htdz.ydkx.wxModelMsg.entity;public class Content {private String value;//消息内容 ...

  8. 实现微信公众号H5消息推送的超级详细步骤

    前言 前段时间在项目中做了一个给H5消息推送的功能,特此记录一下,感兴趣或者有需要的小伙伴可以查阅一下,因为其实代码并不难,我觉得对于初学者来说难的是一些概念和具体实现的过程,所以我会先使用微信提供的 ...

  9. nodejs+express开发微信公众号--配置微信测试号

    1.第一步:申请测试号 地址:https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/inde ...

最新文章

  1. hadoop fs命令无法使用_「大数据」「Hadoop」HDFS的配置与管理
  2. 0x21.搜索 - 树与图的遍历、拓扑排序
  3. 消息队列工具类(MSMQ)
  4. JZOJ 1240. Fibonacci sequence
  5. Android 在添加数据至数据库时添加控制输入语句操作
  6. C++ vector 使用详解
  7. 学点数学(2)-特征函数
  8. Python环境安装脚本,拷贝环境脚本,命令迁移模块(pip freeze requirements.txt)
  9. 多用途bootstrap后台管理系统模板企业统计管理界面模板
  10. java字符串替换 数组,Java工具类-拆分字符串组装数组,替换字符
  11. 分布式存储系统学习笔记(二)—分布式文件系统(4)—内容分发网络(CDN)
  12. 爬取搜狗词库测试可行
  13. VS2005远程调试
  14. STM32状态机编程----什么是状态机?
  15. 如何使用secureCRT连接vmware中的虚拟主机?
  16. 使用python处理视频文件,提取关键帧并保存【已调通】
  17. 联通家庭宽带光猫DDNS设置
  18. USB 协议整理 七:STM32F103之USB概述
  19. 互联网dmz区_服务器设置于DMZ区,DMZ区是什么意思?
  20. 锐意创新,引领音视频未来

热门文章

  1. tyvj 1023 奶牛的锻炼
  2. 2023年天津美术学院专升本专业课考试大纲
  3. 程序员在创业前可以尝试的试炼任务
  4. Python:终端打印字体颜色
  5. Scala获取main函数参数,idea演示
  6. ICMP、TFTP、HTTP、NAT、DHCP、RARP协议的描述
  7. 马斯克要干翻人工智能,“神经蕾丝”能让每个人都能当钢铁侠
  8. 社会工程学的预测展望
  9. 基于SSM的在线课程教学系统
  10. 如何依据激励对象和公司状况,选择正确的股权激励方式?