NodeJS WeChat public account development - Response to Token verification sent by WeChat (Shandong Shumanjianghu)

background

Using NodeJS for WeChat public account development, you first need to respond to the Token verification sent by WeChat, official documents

Fill out the server configuration

Log in to the WeChat public platform and open the page in the basic configuration under development.

Fill in the URL of the interface, the custom Token in turn, click Randomly Generate to generate EncodingAESKey, select the message encryption method as plaintext mode, and WeChat will perform the server's Token verification when clicking Submit.

Response to Token verification sent by WeChat

The project is developed using express, and the middleware wechatAuth is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
var app = express(); 

const crypto = require( 'crypto');
const url = require( 'url');

// perform sha1 encryption
function sha1( str) { let shasum = crypto.createHash( "sha1"); return shasum.update(str, 'utf-8').digest( "hex"); } function wechatAuth( req, res) { let signature = req.query.signature; let echostr = req.query.echostr; let timestamp = req.query.timestamp; let nonce = req.query.nonce; let reqArray = [nonce, timestamp, process.env.WX_TOKEN]; // process.env.WX_TOKEN corresponds to the Token in the server configuration reqArray.sort() ; // sort the array lexicographically













let sortStr = reqArray.join( ''); //join the array let sha1Str = sha1(sortStr.toString().replace( /,/g, "")); if (signature === sha1Str) { res.end (echostr); } else { res.end( "false"); console.log( "Authorization failed!"); } } app.use( '/wx/token',wechatAuth); // Fill in the server configuration accordingly URL










After the server goes online, click the basic configuration to open the submit in this page.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325027246&siteId=291194637