临时素材上传出错,无法获取mediaId
来源:
慕神8321268
2016-09-29
10回答
-
Scott
2016-10-11
这也太没道理了,response.body 是 undefined 说明你用的 request 是比较老一点的版本对吧,把 request 卸载了 npm uninstall request,然后重新装一个 npm install request@latest --save 然后用 response.body 来拿到微信返回的 json 数据,打印出来看看
00 -
Scott
2016-09-29
token 失效了,说明是更新或者获取 token 有问题,最新版的 request 应该是通过 response.body 来获取到 json 数据吧。
把 response[1] 和 response 都改成 var data =response.body 再试试看呢
032016-09-30 -
慕神8321268
提问者
2016-10-06
打印出来的DATA
00 -
慕神8321268
提问者
2016-10-06
wechat.js
'use strict' var Promise = require('bluebird') var request=Promise.promisify(require('request'))//promise化 var util = require('./util') var fs=require('fs') var prefix='https://api.weixin.qq.com/cgi-bin/' var api={ access_token: prefix + 'token?grant_type=client_credential', upload: prefix+'media/upload?' } function Wechat(opts){ var that = this this.appID=opts.appID this.appSecret= opts.appSecret this.getAccessToken=opts.getAccessToken this.saveAccessToken=opts.saveAccessToken //promise实现 this.fetchAccessToken() } Wechat.prototype.fetchAccessToken = function(data) { var that=this //如果有效 if(this.access_token&&this.expires_in){ if(this.isValidAccessToken(this)){ //console.log('data.access_token') //console.log(data.access_token) return Promise.resolve(this) } } //promise实现 return this.getAccessToken() .then(function(data){ try{ data=JSON.parse(data) console.log('fetch_data::::::') console.log(data) console.log('fetch_accesstoken::::::'+data.access_token) } catch(e){ return that.updateAccessToken() } if (that.isValidAccessToken(data)) { return Promise.resolve(data) } else{ return that.updateAccessToken() } }) .then(function (data) { that.access_token=data.access_token that.expires_in=data.expires_in //console.log(that.access_token) //console.log(that.expires_in) that.saveAccessToken(data) return Promise.resolve(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() {//???data var appID= this.appID var appSecret= this.appSecret var url=api.access_token+'&appID='+appID+'&secret='+appSecret return new Promise(function(resolve,reject){ //向服务器发起get请求 request({url: url,json:true}).then(function(response){ var data =response//修改 var now =(new Date().getTime()) var expires_in=now + (data.expires_in - 20)*1000 data.expires_in=expires_in resolve(data) }) }) } Wechat.prototype.uploadMaterial = function(type,filepath) {//???data var that=this var form={ media:fs.createReadStream(filepath) } //var appID= this.appID //var appSecret= this.appSecret return new Promise(function(resolve,reject){ that .fetchAccessToken() .then(function(data){ //console.log(data) //console.log(data.access_token) var url=api.upload+'access_token='+data.access_token+'&type='+type console.log(url) //向服务器发起get请求 request({method:'POST',url: url,formData:form,json:true}).then(function(response){ var _data =response //console.log(response[1]) //console.log("__________________________________________________________") //console.log(response.body) //console.log(_data) if(_data){ resolve(_data) } else{ throw new Error('upload material fails') } }) .catch(function(err){ reject(err) }) }) }) } Wechat.prototype.reply = function() { var content = this.body var message = this.weixin console.log(content); var xml = util.tpl(content,message) this.status = 200 this.type = 'application/xml' this.body = xml }; module.exports=Wechat
042016-10-11 -
慕神8321268
提问者
2016-10-05
response[1]部分在横线上方,
response.body 打印结果在横线下
022016-10-06 -
慕神8321268
提问者
2016-09-30
我自己打印了一下,发现uploadMaterial能获取到data,但是无法得到票据access_token,不知道是什么原因,我将wechat的代码贴上来麻烦大神帮我看看~
'use strict' var Promise = require('bluebird') var request=Promise.promisify(require('request'))//promise化 var util = require('./util') var fs=require('fs') var prefix='https://api.weixin.qq.com/cgi-bin/' var api={ access_token: prefix + 'token?grant_type=client_credential', upload: prefix+'media/upload?' } function Wechat(opts){ var that = this this.appID=opts.appID this.appSecret= opts.appSecret this.getAccessToken=opts.getAccessToken this.saveAccessToken=opts.saveAccessToken //promise实现 this.fetchAccessToken() } Wechat.prototype.fetchAccessToken = function(data) { var that=this //如果有效 if(this.access_token&&this.expires_in){ if(this.isValidAccessToken(this)){ return Promise.resolve(this) } } //promise实现 return this.getAccessToken() .then(function(data){ try{ data=JSON.parse(data) } catch(e){ return that.updateAccessToken() } if (that.isValidAccessToken(data)) { return 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) return Promise.resolve(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() {//???data var appID= this.appID var appSecret= this.appSecret var url=api.access_token+'&appID='+appID+'&secret='+appSecret return new Promise(function(resolve,reject){ //向服务器发起get请求 request({url: url,json:true}).then(function(response){ var data =response//修改 //console.log(data) var now =(new Date().getTime()) var expires_in=now + (data.expires_in - 20)*1000 data.expires_in=expires_in resolve(data) }) }) } Wechat.prototype.uploadMaterial = function(type,filepath) {//???data var that=this var form={ media:fs.createReadStream(filepath) } //var appID= this.appID //var appSecret= this.appSecret return new Promise(function(resolve,reject){ that .fetchAccessToken() .then(function(data){ console.log(data) console.log(data.access_token) var url=api.upload+'access_token='+data.access_token+'&type='+type console.log(url) //向服务器发起get请求 request({method:'POST',url: url,formData:form,json:true}).then(function(response){ var _data =response //console.log(_data) if(_data){ resolve(_data) } else{ throw new Error('upload material fails') } }) .catch(function(err){ reject(err) }) }) }) } Wechat.prototype.reply = function() { var content = this.body var message = this.weixin console.log(content); var xml = util.tpl(content,message) this.status = 200 this.type = 'application/xml' this.body = xml }; module.exports=Wechat
062016-10-05 -
慕神8321268
提问者
2016-09-30
修改response之后出现的错误如图
00 -
慕神8321268
提问者
2016-09-29
tpl.js
'use strict' var ejs=require('ejs')//模板引擎 var heredoc=require('heredoc') var tpl=heredoc(function () {/* <xml> <ToUserName><![CDATA[<%= toUserName %>]]></ToUserName> <FromUserName><![CDATA[<%= fromUserName %>]]></FromUserName> <CreateTime><%= createTime %></CreateTime> <MsgType><![CDATA[<%= msgType %>]]></MsgType> <% if (msgType==='text') { %> <Content><![CDATA[<%= content %>]]></Content> <% } else if (msgType==='image') { %> <Image> <MediaId><![CDATA[<%= content.mediaId %>]]></MediaId> </Image> <% } else if (msgType==='voice') { %> <Voice> <MediaId><![CDATA[<%= content.mediaId %>]]></MediaId> </Voice> <% } else if (msgType==='video') { %> <Video> <MediaId><![CDATA[<%= content.mediaId %>]]></MediaId> <Title><![CDATA[<%= content.title %>]]></Title> <Description><![CDATA[<%= content.description %>]]></Description> </Video> <% } else if (msgType==='music') { %> <Music> <Title><![CDATA[<%= content.title %>]]></Title> <Description><![CDATA[<%= content.description %>]]></Description> <MusicUrl><![CDATA[<%= content.musicUrl %>]]></MusicUrl> <HQMusicUrl><![CDATA[<%= content.hqMusicUrl %>]]></HQMusicUrl> <ThumbMediaId><![CDATA[<%= content.ThumbMediaId %>]]></ThumbMediaId> </Music> <% } else if (msgType==='news') { %> <ArticleCount><%= content.length %></ArticleCount> <Articles> <% content.forEach(function(item){ %> <item> <Title><![CDATA[<%= item.title %>]]></Title> <Description><![CDATA[<%= item.description %>]]></Description> <PicUrl><![CDATA[<%= item.picUrl %>]]></PicUrl> <Url><![CDATA[<%= item.url %>]]></Url> </item> <% }) %> </Articles> <% } %> </xml> */}) var compiled=ejs.compile(tpl)//编译这个函数 exports=module.exports={ compiled:compiled }
00 -
慕神8321268
提问者
2016-09-29
wechat.js
'use strict' var Promise = require('bluebird') var request=Promise.promisify(require('request'))//promise化 var util = require('./util') var fs=require('fs') var prefix='https://api.weixin.qq.com/cgi-bin/' var api={ access_token: prefix + 'token?grant_type=client_credential', upload: prefix+'media/upload?' } function Wechat(opts){ var that = this this.appID=opts.appID this.appSecret= opts.appSecret this.getAccessToken=opts.getAccessToken this.saveAccessToken=opts.saveAccessToken //promise实现 this.fetchAccessToken() } Wechat.prototype.fetchAccessToken = function(data) { var that=this //如果有效 if(this.access_token&&this.expires_in){ if(this.isValidAccessToken(this)){ return Promise.resolve(this) } } //promise实现 return this.getAccessToken() .then(function(data){ try{ data=JSON.parse(data) } catch(e){ return that.updateAccessToken(data) } if (that.isValidAccessToken(data)) { return 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) return Promise.resolve(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() {//???data var appID= this.appID var appSecret= this.appSecret var url=api.access_token+'&appID='+appID+'&secret='+appSecret return new Promise(function(resolve,reject){ //向服务器发起get请求 request({url: url,json:true}).then(function(response){ var data =response//修改 var now =(new Date().getTime()) var expires_in=now + (data.expires_in - 20)*1000 data.expires_in=expires_in resolve(data) }) }) } Wechat.prototype.uploadMaterial = function(type,filepath) {//???data var that=this var form={ media:fs.createReadStream(filepath) } //var appID= this.appID //var appSecret= this.appSecret return new Promise(function(resolve,reject){ that .fetchAccessToken() .then(function(data){ var url=api.upload+'access_token='+data.access_token+'&type='+type //向服务器发起get请求 request({method:'POST',url: url,formData:form,json:true}).then(function(response){ var _data =response[1] console.log(_data) if(_data){ resolve(_data) } else{ throw new Error('upload material fails') } }) .catch(function(err){ reject(err) }) }) }) } Wechat.prototype.reply = function() { var content = this.body var message = this.weixin console.log(content); var xml = util.tpl(content,message) this.status = 200 this.type = 'application/xml' this.body = xml }; module.exports=Wechat
00 -
慕神8321268
提问者
2016-09-29
weixin.js
'use strict' var config=require('./config') var Wechat=require('./wechat/wechat') var wechatApi=new Wechat(config.wechat) exports.reply=function*(next){ var message = this.weixin if(message.MsgType==='event'){ if (message.Event==='subscribe'){ if(message.EventKey){ console.log('扫描二维码'+message.EventKey+''+message.Ticket) } this.body='哈哈,你订阅了这个号\r\n'+'消息ID:'+message.MsgId } else if(message.Event==='unsubscribe'){ console.log('无情取关') this.body(' ') } else if(message.Event==='LOCATION'){//定位 this.body='您上报的地理位置是'+message.Latitude+'/'+message.Longitude+'-'+message.Precision } else if(message.Event==='CLICK'){ this.body='您点了菜单: '+message.EventKey } else if(message.Event==='SCAN'){ console.log('关注后扫二维码'+message.EventKey+' '+message.Ticket) this.body='看到你扫一下哦' } else if(message.Event==='VIEW'){ this.body='您点击了菜单中的链接'+message.EventKey//菜单的URL地址 } } else if (message.MsgType==='text'){ var content=message.Content var reply='你说的'+message.Content+'太复杂了' //回复策略 if(content==='1'){ reply='第一没有啊' } else if(content==='2'){ reply='第二也没有啊' } else if(content==='3'){ reply='第三还没有啊' } else if(content==='4'){//回复图文 reply=[{ title: '技术改变世界', description: '只是个描述', picUrl: 'http://a0.att.hudong.com/50/85/20300534894500139519859925271.jpg', url:'https://console.qcloud.com/cvm' }] } else if(content==='5'){ var data=yield wechatApi.uploadMaterial('image',__dirname+'/2.jpg') reply={ type:'image', mediaId: data.media_id } //console.log(reply) } this.body=reply } yield next }
00
相似问题