国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 課堂 > 小程序 > 正文

授權(quán)微信第三方平臺新手教程

2020-03-21 16:19:36
字體:
供稿:網(wǎng)友

授權(quán)微信第三方平臺新手教程。

近期微信更新了第三方平臺,小程序可以授權(quán)第三方,看了一些接口,把所學(xué)的做下記錄。
想用第三方,微信公眾號需申請認(rèn)證,然后在公眾號平臺上創(chuàng)建第三方,這些流程我都沒走過,這就不多說了。

登錄公眾賬號 -> 管理中心 -> 第三方平臺 -> (某個第三方)詳情 -> 修改平臺(可通過這里修改)

其中會看到如下:

微信第三方平臺,微信第三方平臺授權(quán),微信第三方平臺開發(fā)

微信第三方平臺,微信第三方平臺授權(quán),微信第三方平臺開發(fā)

微信第三方平臺,微信第三方平臺授權(quán),微信第三方平臺開發(fā)

  • 授權(quán)事件接收URL: 是后面獲取微信推送的component_verify_ticket
  • 公眾號消息校驗Token:驗證消息體簽名
  • 公眾號消息加解密Key:加解密
  • 白名單IP地址列表:服務(wù)器IP或訪問的IP

    接著綁定小程序(點擊添加開發(fā)小程序進(jìn)入以下)

    微信第三方平臺,微信第三方平臺授權(quán),微信第三方平臺開發(fā)

    注意綁定后要把 原始Id 添加進(jìn) 授權(quán)測試公眾號列表,其他項也是要填寫的,只不過我覺得紅色框比較重要些。

    授權(quán)流程圖(部分接口)

    微信第三方平臺,微信第三方平臺授權(quán),微信第三方平臺開發(fā)

    其中應(yīng)該注意的是 授權(quán)碼換取2個token 和 微信登錄 這兩個接口。

    注意:

    1、pre_auth_code 和 auth_code 是不一樣的,pre_auth_code 是預(yù)授權(quán)碼,經(jīng)過用戶進(jìn)入授權(quán)頁掃二維碼后授權(quán),跳轉(zhuǎn)回調(diào)返回給你的才是授權(quán)碼(auth_code)。

    2、微信登錄url帶的 component_access_token 確實就是 component_access_token,并不是像微信參數(shù)說明里的“小程序授權(quán)的authorizer_access_token”。

    //本人首先用的是 authorizer_access_token ,結(jié)果在小程序里老是抱錯 "{errcode: 48001, errmsg: 'api unauthorized', ...}"

    獲取 component_verify_ticket

    消息體解密流程圖:

    微信第三方平臺,微信第三方平臺授權(quán),微信第三方平臺開發(fā)

    Token 和 EncodingAESKey 為上面所填寫的Token和Key。

    1. 接收json數(shù)據(jù)

    可用'body-parser-xml'這插件,把下面代碼加進(jìn)server.js 或者 app.js (運行文件)

    ...require('body-parser-xml')(bodyParser)server.use(bodyParser.xml({  limit: '2MB',  xmlParseOptions: {    normalize: true,    normalizeTags: true,    explicitArray: false  }}))...

    2. 驗證簽名

    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'

    3. AES解密

    • PKCS#7填充刪除

      PKCS#7:K為秘鑰字節(jié)數(shù)(采用32),buf為待加密的內(nèi)容,N為其字節(jié)數(shù)。Buf 需要被填充為K的整數(shù)倍。在buf的尾部填充(K-N%K)個字節(jié),每個字節(jié)的內(nèi)容 是(K- N%K)。

      var pkcs7Encoder = {  //對需要加密的明文進(jìn)行填充補位  encode: function(text) {    var blockSize = 32    var textLength = text.length    //計算需要填充的位數(shù)    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 轉(zhuǎn) 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的內(nèi)容長度的網(wǎng)絡(luò)字節(jié)序    var msgLength = new Buffer(4)    //寫入無符號32位整型,大端對齊    msgLength.writeUInt32BE(msg.length, 0)    var bufMsg = Buffer.concat([randomString, msgLength, msg, id])    // 對明文進(jìn)行補位操作    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()])    // 返回加密數(shù)據(jù)的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() 緩沖區(qū)合并      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插件 轉(zhuǎn)化即可得到我們想要的 json 格式了。

        var xml2jsonString = require('xml2js').parseStringxml2jsonString(xml, {async:true}, (err, json)=> {  if(err) return console.log(err)  console.log(json)})

        到這解密算是完成了。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 渑池县| 阿拉善盟| 乌什县| 昔阳县| 绍兴县| 定边县| 富蕴县| 玉树县| 和平县| 汾西县| 合江县| 河间市| 黄山市| 湛江市| 屏山县| 红河县| 繁峙县| 定襄县| 巫溪县| 双峰县| 鹤壁市| 肥东县| 全州县| 永嘉县| 怀来县| 镇江市| 彝良县| 大城县| 文登市| 两当县| 茶陵县| 龙泉市| 南岸区| 同仁县| 马公市| 迁西县| 乡城县| 厦门市| 邵阳市| 西畴县| 天津市|