2-7课程前半节课的XML数据出不来
来源:
qq_Misswhere_03404569
2016-09-08
2-6的代码顺利完成,accesstoken也得到了。但是2-7中按照老师步骤下来,关注和取消关注公众号会在命令行里输出XML数据,但是我的没反应!只输出了端口1234,也不报错。
这是g.js
'use strict' var sha1 = require('sha1'); var getRawBody = require('raw-body'); var Wechat = require('./wechat'); //var util = require('./util.js'); module.exports = function(opts) { //var wechat = new Wechat(opts); return function *(next) { var token = opts.token; var signature = this.query.signature; var nonce = this.query.nonce; var timestamp = this.query.timestamp; var echostr = this.query.echostr; var str = [token, timestamp, nonce].sort().join(''); var sha = sha1(str); if (this.method === 'GET') { if (sha === signature) { this.body = echostr + ''; } else { this.body = 'wrong'; } } else if (this.method === 'POST') { if (sha !== signature) { this.body = 'wrong'; return false; } var data = yield getRawBody(this.req, { length: this.length, limit: '1mb', encoding: this.charset }); console.log(data.toString()); // var content = yield util.parseXMLAsync(data); // console.log(content); } } }
这是wechat.js
'use strict' var Promise = require('bluebird'); var request = Promise.promisify(require('request')); var prefix = 'https://api.weixin.qq.com/cgi-bin/'; var api = { accessToken: prefix + 'token?grant_type=client_credential' } function Wechat(opts) { var that = this; this.appID = opts.appID; this.appSecret = opts.appSecret; this.getAccessToken = opts.getAccessToken; this.saveAccessToken = opts.saveAccessToken; this.getAccessToken() .then(function(data) { try { data = JSON.parse(data); } catch(e) { return that.updateAccessToken(); } if(that.isValidAccessToken(data)) { Promise.resolve(data); } else { return that.updateAccessToken(); } }) .then(function(data) { that.access_token = data.access_token; that.expires_in = data.expires_in; that.saveAccessToken(data); }) } Wechat.prototype.isValidAccessToken = function(data) { if (!data || !data.access_token || !data.expires_in) { return false; } var access_token = data.access_token; var expires_in = data.expires_in; var now = (new Date().getTime()); if (now < expires_in) { return true; } else { return false; } } Wechat.prototype.updateAccessToken = function() { var appID = this.appID; var appSecret = this.appSecret; var url = api.accessToken + '&appid=' + appID + '&secret=' + appSecret; return new Promise(function(resolve, reject) { request({url: url, json:true}).then(function(response) { var data = response.body; var now = (new Date().getTime()); var expires_in = now + (data.expires_in - 20) * 1000; data.expires_in = expires_in; resolve(data); }); }); } module.exports = Wechat;
写回答
5回答
-
目测代码没问题,这样子的话,是 微信推送的,用来验证开发者身份的 URL 请求,并没有进入到本地 localhost:1234 的后台 Koa 服务中,从而在
return function *(next) { var token = opts.token;
这里面的代码根本就没执行。
你从本地浏览器直接输入:
localhost:1234/?signature=1&nonce=2&signature=3&echostr=4
看后台能不能接收到这个请求,打印出这些 query 参数,如果可以的话,说明你在公众号里面填的代理地址,很可能出问题了,导致代理不到本地,这里尽量用 QQ 浏览器做代理哈(后面章节有提到用 QQ 浏览器代理端口)
012016-09-09 -
Scott
2016-09-08
Promise.resolve(data); } else { return that.updateAccessToken(); }
这里加上 return
return Promise.resolve(data);
012016-09-08 -
qq_Misswhere_03404569
提问者
2016-09-08
00 -
qq_Misswhere_03404569
提问者
2016-09-08
'use strict' var Koa = require('koa'); var path = require('path'); var wechat = require('./wechat/g'); var util = require('./libs/util'); var wechat_file = path.join(__dirname, './config/wechat.txt'); var config = { wechat: { appID:'wx3afe79fdc599c3f3', appSecret:'dec89db3bfbe057747ee7bc92ff2c59f', token:'yanchenhuiweixin666', getAccessToken:function() { return util.readFileAsync(wechat_file); }, saveAccessToken:function(data) { data = JSON.stringify(data); return util.writeFileAsync(wechat_file, data); } } } var app = new Koa(); app.use(wechat(config.wechat)); app.listen(3000); console.log('Listening:3000');
00 -
qq_Misswhere_03404569
提问者
2016-09-08
我在g.js的第11行改成
return function *(next) { console.log(1); var token = opts.token;
关注后也没输出1
022016-09-08
相似问题
关注和取消都获取不到XML数据
回答 4
老师,课程不是完整的吧
回答 3