授權微信第三方平臺新手教程。
近期微信更新了第三方平臺,小程序可以授權第三方,看了一些接口,把所學的做下記錄。
想用第三方,微信公眾號需申請認證,然后在公眾號平臺上創建第三方,這些流程我都沒走過,這就不多說了。
其中會看到如下:
接著綁定小程序(點擊添加開發小程序進入以下)
注意綁定后要把 原始Id 添加進 授權測試公眾號列表,其他項也是要填寫的,只不過我覺得紅色框比較重要些。
其中應該注意的是 授權碼換取2個token 和 微信登錄 這兩個接口。
//本人首先用的是 authorizer_access_token ,結果在小程序里老是抱錯 "{errcode: 48001, errmsg: 'api unauthorized', ...}"
消息體解密流程圖:
Token 和 EncodingAESKey 為上面所填寫的Token和Key。
可用'body-parser-xml'這插件,把下面代碼加進server.js 或者 app.js (運行文件)
...require('body-parser-xml')(bodyParser)server.use(bodyParser.xml({ limit: '2MB', xmlParseOptions: { normalize: true, normalizeTags: true, explicitArray: false }}))...
var crypto = require('crypto')var sha1 = crypto.createHash('sha1'), dev_msg_signature = sha1.update( [token, timestamp, nonce, msg_encrypt].sort().join('') ).digest('hex') if(dev_msg_signature === msg_signature) return 'pass'
PKCS#7:K為秘鑰字節數(采用32),buf為待加密的內容,N為其字節數。Buf 需要被填充為K的整數倍。在buf的尾部填充(K-N%K)個字節,每個字節的內容 是(K- N%K)。
var pkcs7Encoder = { //對需要加密的明文進行填充補位 encode: function(text) { var blockSize = 32 var textLength = text.length //計算需要填充的位數 var amountToPad = blockSize - (textLength % blockSize) var result = new Buffer(amountToPad) result.fill(amountToPad) return Buffer.concat([text, result]) //尾部填充 }, //刪除解密后明文的補位字符 decode: function(text) { var pad = text[text.length - 1] if (pad < 1 || pad > 32) { pad = 0 } return text.slice(0, text.length - pad) }}module.exports = pkcs7EncoderAES加解密算法為'aes-256-cbc', 需要的 Key 及 ivKey:EncodingAESKey + '='iv:EncodingAESKey 轉 Buffer 的前16位var crypto = require('crypto') , pkcs7Encoder = require('./pkcs7Encoder')class wxBizMsgCrypto { constructor(encodingAesKey, appId) { if(!encodingAesKey || !appId) { throw new Error('please check arguments') } var AESKey = new Buffer(encodingAesKey + '=', 'base64') if(AESKey.length !== 32) { throw new Error('encodingAESKey invalid') } this.AESKey = AESKey this.iv = AESKey.slice(0, 16) this.appId = appId } encryptMsg(text) { // 獲取16B的隨機字符串 var randomString = crypto.pseudoRandomBytes(16) , msg = new Buffer(text) , id = new Buffer(this.appId) // 獲取4B的內容長度的網絡字節序 var msgLength = new Buffer(4) //寫入無符號32位整型,大端對齊 msgLength.writeUInt32BE(msg.length, 0) var bufMsg = Buffer.concat([randomString, msgLength, msg, id]) // 對明文進行補位操作 var encoded = pkcs7Encoder.encode(bufMsg) var cipher = crypto.createCipheriv('aes-256-cbc', this.AESKey, this.iv); cipher.setAutoPadding(false) var cipheredMsg = Buffer.concat([cipher.update(encoded), cipher.final()]) // 返回加密數據的base64編碼 return cipheredMsg.toString('base64') } decryptMsg(resXml) { var msg_encrypt = resXml.encrypt try { var decipher = crypto.createDecipheriv('aes-256-cbc', this.AESKey, this.iv) decipher.setAutoPadding(false) //Buffer.concat() 緩沖區合并 var deciphered = Buffer.concat([decipher.update(msg_encrypt, 'base64'), decipher.final()]) deciphered = pkcs7Encoder.decode(deciphered) var content = deciphered.slice(16) , length = content.slice(0, 4).readUInt32BE(0) } catch (err) { throw new Error(err) } return { msgXml: content.slice(4, length + 4).toString(), appid: content.slice(length + 4).toString() } }}module.exports = wxBizMsgCrypto
最后獲取到的xml再通過'xml2js'這 npm插件 轉化即可得到我們想要的 json 格式了。
var xml2jsonString = require('xml2js').parseStringxml2jsonString(xml, {async:true}, (err, json)=> { if(err) return console.log(err) console.log(json)})
到這解密算是完成了。
新聞熱點
疑難解答