mediaId无法查询
来源:4-4 第四天 上传临时素材之图片和视频
凉夏暖冬
2016-07-23
'use strict' var Promise =require('bluebird') var request = Promise.promisify(require('request')) var util=require('./util') var fs =require('fs') var prefix = 'https://api.weixin.qq.com/cgi-bin/' var api={ accessToken: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 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) } } 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(){ 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) }) }) } Wechat.prototype.uploadMaterial = function(type,filepath){ 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 request({method:'POST',url:url,formData: form,json:true}).then(function (response) { var _data = response.body if(_data){ resolve(_data) }else { throw new Error('Upload meterial fails') } }) .catch(function (err) { reject(err) }) }) }) } Wechat.prototype.reply=function () { var content = this.body var message = this.weixin var xml= util.tpl(content,message) this.status=200 this.type='application/xml' this.body=xml } module.exports=Wechat
'use strict'
var config = require('./config')
var Wechat = require('./wechat/wechat')
var path = require('path')
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 = '欢迎订阅这个号'
}
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
}
}
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:__dirname+'./img/1.jpg',
url:'https://github.com/'
},{
title:'知识改变世界',
description:'卖弄书',
picUrl:__dirname+'./img/1.jpg',
url:'https://github.com/'
}]
}
else if(content==='5'){
var data = yield(wechatApi.uploadMaterial('image',path.join(__dirname, '/img/2.jpg')))
reply={
type:'image',
mediaId:data.media_id
}
}
this.body=reply
}
yield (next)
}
1回答
-
else if(content==='5'){ var data = yield(wechatApi.uploadMaterial('image',path.join(__dirname, '/img/2.jpg'))) reply={ type:'image', mediaId:data.media_id } } this.body=reply }
console.log()打印一下获取到的data,看看有没有media_id,或者是错误信息。
我自己申请了个订阅号,账号主体是个人的,没办法通过微信认证,有很多接口权限都没开放。所以就无法上传素材,也就获取不到media_id,而是返回了一个错误信息:errMsg:"48001 api unauthorized",提示接口未授权。
如果楼主也遇到接口未授权的问题,可以申请一个测试号,里面开放各种接口。
012016-11-04
相似问题