收藏
回答

上传图片报签名错误,其他接口签名是正确的,请大佬帮忙看下?,非常感谢!

let res = await cloud.downloadFile({ fileID: event.fileID })

let filebuffer = res.fileContent

let filename = event.filename

let sha256 = crypto.createHash('sha256').update(filebuffer).digest('hex')

var body = JSON.stringify({

'filename': filename, 'sha256': sha256

})

let method = 'POST'

let url = '/v3/merchant/media/upload'

// let jsonStr = JSON.stringify({ filename, sha256 })

var timestamp = Date.parse(new Date()) / 1000

const randomstr = '95E9D72DB0389359EB54850779354B21'

var data = method + '\n' +

url + '\n' +

timestamp + '\n' +

randomstr + '\n' +

body + '\n'

// console.log('data>>>>>>>>>>>>>>>>>',data)

let sign = crypto.createSign('RSA-SHA256');

sign.update(new Buffer(data, 'utf-8'));

let sig = sign.sign(privateKey, 'base64');

console.log(sig)

body = '--boundary'+'\n'+'\r\n'

+'Content-Disposition: form-data; name="meta";'+'\n'+'\r\n'

+'Content-Type: application/json' + '\n'+'\r\n'

+body+'\n'+'\r\n'

+'--boundary'+'\n'+'\r\n'

+'Content-Disposition: form-data; name="file"; filename="'+filename+'";'+'\n'+'\r\n'

+'Content-Type: image/png'+'\n'+'\r\n'

+'\n'+'\r\n'

+filebuffer+'\n'+'\r\n'

+'--boundary--'+'\n'+'\r\n'

console.log(body)

// //////////////////////////////////////////

var mchid = 'xxxxxx'

var serial_no = 'xxxxxxxxx'

var options = {

method: method,

headers: {

'Accept': 'application/json',

// 'Content-Type':'application/json',

'Content-Type': 'multipart/form-data;boundary=boundary',

'Accept-Charset': 'utf-8',

'User-Agent': 'my-reddit-client',

'Authorization': 'WECHATPAY2-SHA256-RSA2048 mchid="'+mchid+'",nonce_str="'+randomstr+'",signature="'+sig+'",timestamp="'+timestamp+'",serial_no="'+serial_no+'"'

},

body: body,

uri: "https://api.mch.weixin.qq.com"+url

};

return rp(options).then(

function (res) {

console.log(res)

return res

}

).catch(

function (err) {

console.log(err)

return err

}

)


回答关注问题邀请回答
收藏

2 个回答

  • JUNL1u19er
    JUNL1u19er
    2020-11-13

    'Content-Type': 'multipart/form-data;boundary=boundary',

    应该是'Content-Type': 'multipart/form-data;boundary='+key,

    可以看看这个例子https://juejin.im/post/6844903752604844045

    2020-11-13
    有用
    回复
  • 北望沣渭
    北望沣渭
    2020-09-16

    补/替换一下你的代码:

    let timestamp |=0;
    let sign = crypto.createSign('sha256WithRSAEncryption');
    


    PS:你有没有尝试过 form-data & wechatpay-axios-plugin 包 上传文件?

    2020-09-16
    有用
    回复 2
    • 守望
      守望
      2020-09-16
      我试了,但是结果返回是null


      下面是我的代码,不知哪里有问题?
      2020-09-16
      回复
    • 守望
      守望
      2020-09-16回复守望
      云函数报中的错误:
      2020-09-16
      回复
登录 后发表内容
问题标签