微信公众号,服务器配置,token一直验证失败,怎么帮?
如下图:
appID为wxaa0fd8e7b03bd9ec
最近操作时间为20191221 2123
请帮忙解决下,谢谢!
3 回复
米法(BINNIE SMILE) 谢谢,代码如下:
控制器代码:
import { Controller, Get, Header, Query } from '[@nestjs](/user/nestjs)/common';
import { WechatService } from './wechat.service';
[@Controller](/user/Controller)('wechat')
export classWechatController{
constructorprivate readonly wechatService: WechatService) {}
[@Get](/user/Get)()
[@Header](/user/Header)('Access-Control-Allow-Origin', '*')
[@Header](/user/Header)('Access-Control-Allow-Headers', 'X-Requested-With')
[@Header](/user/Header)('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS')
[@Header](/user/Header)('X-Powered-By', '3.2.1')
[@Header](/user/Header)('Content-Type', 'text/plain;charset=utf-8')
public async receiveMsgFromWechat(
[@Query](/user/Query)() query,
) {
const result = this.wechatService.verify(
query.signature,
query.timestamp,
query.nonce,
query.echostr,
);
// console.log(`query is ${JSON.stringify(query)}`);
let res;
if (result['isPass']) {
// res = {
// status: 200,
// body: result['data'],
// };
return result['data'];
} else {
res = {
status: '403',
msg: result['message'],
};
console.log('res类型是', typeof res);
return res;
}
}
}
服务:
import { Injectable } from'[@nestjs](/user/nestjs)/common';
import * as crypto from'crypto';
[@Injectable](/user/Injectable)()
exportclass WechatService {
// 验证
public verify = signature, timestamp, nonce, echostr) => {
// 约定token
const config = {
wechat: {
appID: '',
appsecret: '',
token: '',
},
};
// 生成密钥
const token = config.wechat.token;
const str = [token, timestamp, nonce].sort().join();
const sha1Code = crypto.createHash('sha1');
const sha = sha1Code.update(str, 'utf8').digest('hex');
let result = {};
console.log('检查signature和sha', signature, sha);
if (sha === signature) {
// 成功
result['isPass'] = true;
result['data'] = echostr;
} else {
// 失败
result['isPass'] = false;
result['message'] = '验证失败!重新再试';
}
return result;
};
}
进程管理使用pm2,在验证服务器的时候,没有打印日志
:(