配置NodeJs服务作为验证微信公众号token

在公众号基本配置的时候,需要在服务端校验token

image.png

url: 填写下一篇文章中的外网域名,
token: 可以自定义,与服务端的对应
EncodingAESKey: 随机生成就好了

PS: 以上的简单的提下,具体请看第三章。

搭建服务( node + koa),来作为微信公众号的服务器,前提是需要有一个微信公众号

'use strict'
const Koa = require('koa');
const sha1 = require('sha1');
const config = {
  wechat: {
    appID: 'wx01871fde9e26191c', //公众号里面取
    AppSecret: 'ab38f22e60ebb0a911d461e9b7be8ad6', //公众号里面取
    token: 'ab38f22e60ebb0a911d461hello' //自定义的token
  }
}

var app = new Koa();
app.use(function *(next){
  console.log(this.query);

  const token = config.wechat.token;
  const signature = this.query.signature;
  const nonce = this.query.nonce;
  const timestamp = this.query.timestamp;
  const echostr = this.query.echostr;

  let str = [token, timestamp, nonce].sort().join('');
  let sha = sha1(str);
  console.log(sha, signature);
  if(sha == signature){
    this.body = echostr;
  } else {
    this.body = 'wrong';
  }
});
app.listen(80);
console.log('listen 80');
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、公众号介绍 微信公众号分类 订阅号:主要偏于为用户传达资讯(类似报纸杂志),认证前后都是每天只可以群发一条消息...
    小花的胖次阅读 6,627评论 3 37
  • 开发前首先我们要知道一些概念 各公众号区别:1、订阅号:为媒体和个人提供一种信息传播方式,主要偏于为用户传达资讯(...
    CoderZS阅读 3,244评论 1 19
  • 微信服务号开发 整体流程 域名报备,服务器搭建 Python开发环境和项目的初始化搭建; 微信公众号注册及开发模式...
    飞行员suke阅读 4,612评论 0 14
  • Part 1. 验证公众号 (开发者) 名词解释 timestamp 时间戳 nonce 随机数 token+ti...
    _v_xw阅读 1,092评论 1 1
  • 我是一个不懂得拒绝的人。 于我而言,说一个“不”字比写一篇5000字的小说还要难。归根结底,是因为自己本身就是害怕...
    悟恩说事阅读 858评论 2 1