wechat icon indicating copy to clipboard operation
wechat copied to clipboard

消息加解密方式 安全模式 失败

Open tonyleecn opened this issue 8 years ago • 12 comments

在公众号中设置安全模式或兼容模式都不能被动回复成功(text类型) 但是在微信公众平台接口调试工具调试是成功的。 通过运维中心看到的日志

请求表单数据(解密后)

<ToUserName></ToUserName> <FromUserName></FromUserName> <CreateTime>1469610839</CreateTime> <MsgType></MsgType> <Content></Content> <MsgId>6311930491764374545</MsgId> 被动回复内容

<Encrypt></Encrypt><MsgSignature></MsgSignature><TimeStamp>1469610775634</TimeStamp><Nonce></Nonce> 被动回复内容(解密后)

tonyleecn avatar Jul 27 '16 09:07 tonyleecn

没有遇见过不成功的情况

JacksonTian avatar Jul 27 '16 09:07 JacksonTian

能复现方法么。

JacksonTian avatar Jul 27 '16 10:07 JacksonTian

var app = express();

// set up handlebars view engine var handlebars = require('express3-handlebars').create({ defaultLayout:'main', helpers: { section: function(name, options){ if(!this._sections) this._sections = {}; this._sections[name] = options.fn(this); return null; } } }); app.engine('handlebars', handlebars.engine);

app.set('view engine', 'handlebars');

app.set('port', process.env.PORT || 3391);

app.use(express.static(__dirname + '/public'));

app.use(express.query());

app.use('/wechat', wechat(config, function (req, res, next) { // 微信输入信息都在req.weixin上 var message = req.weixin; console.log(message); if (message.MsgType === 'text') { if (message.Content === 'hello') { res.reply("你好"); } else { // 回复屌丝(普通回复) // res.reply('你好!'); res.reply({type: "text", content: 'Hello 好吧!'}); } } }));

// app.get('/', function(req, res) { // res.render('home'); // });

app.listen(app.get('port'), function(){ console.log( 'Express started on http://localhost:' + app.get('port') + '; press Ctrl-C to terminate.' ); });

tonyleecn avatar Jul 27 '16 10:07 tonyleecn

用明文方式就是正常的

版本 "dependencies": { "express": "^4.14.0", "express3-handlebars": "^0.5.2", "wechat": "^2.0.3", "wechat-api": "^1.28.0", "weui": "^0.4.3" }

用了nginx跳转 server { listen 80; server_name weixin.********.com; root /home/webroot;

#charset koi8-r;
#access_log  /var/log/nginx/log/host.access.log  main;

access_log /var/log/nginx/weixin.log main;
error_log /var/log/nginx/weixin..error.log error;


location / {
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://192.168.1.60:7890;
    add_header   Cache-Control no-cache;
}

}

tonyleecn avatar Jul 27 '16 10:07 tonyleecn

我也碰到这问题了

atjiu avatar Aug 27 '16 23:08 atjiu

确认参数传对了吗

JacksonTian avatar Aug 28 '16 06:08 JacksonTian

只要修改为明文的就可以正常使用,参数没有问题 不知道是不是测试账号的问题?

tonyleecn avatar Aug 30 '16 08:08 tonyleecn

@JacksonTian @tonyleecn 我用的是未认证的公众号,不是测试账号,也是这个问题

atjiu avatar Aug 30 '16 12:08 atjiu

在配置服务器的时候根本就验证不了,就是根本没有回复微信服务器post过来的值。

szwork2013 avatar Oct 05 '16 20:10 szwork2013

我也遇到同样的问题。

jarvisji avatar Apr 08 '17 06:04 jarvisji

知道原因了,问题发生在本地调试,post的data和server不同。本地只post了基本的消息xml,而实际wechat发过来的数据远不止这么多。 wechat/lib/wechat.js

        var xml = formatMessage(result.xml);
        var encryptMessage = xml.Encrypt;    //这里本地调试的时候是undefined.
        if (signature !== cryptor.getSignature(timestamp, nonce, encryptMessage)) {
          res.writeHead(401);
          res.end('Invalid signature');
          return;
        }

另外,wechat,wechat-api等几个lib非常棒,感谢作者。

jarvisji avatar Apr 08 '17 07:04 jarvisji

遇到同样的问题. res.reply非空字符串以及其它,公众号直接报不能服务。 微信调试工具显示success. res.reply空字符串公众号,能正常不回复。

wyvern92 avatar Aug 10 '17 12:08 wyvern92