Micro-channel JS-SDK "share information setting" digital signature generation method and the API "

step


 

The first step in certified micro-channel public account

First you have to have a micro-channel public number or a certified developer account, there is no certified public account. Digital signature authentication can be successful, but sharing information is not set up successful;
 

The second step is added security domain

Add app running in the background domain name address public account platform, it can be interpreted as adding to whitelist feature for a domain name
To our company's banyan tree as an example:
Micro-channel public number is: under the banyan tree, under game.4gshu.com added digital security domain, then I pages on game.4gshu.com can use the signature banyan public accounts
 

The third step is to generate a digital signature

Appid and find a secret string above the background micro-channel public platform
By these data, a request address two public API provided by micro-channel, and then generate a corresponding access_token ticket generated by the rule and then encrypted into a digital signature
Note that the digital signature must be generated on the server side, and here I come to realize NodeJS
Specific digital signature generation process, NodeJS version
1, to obtain the required micro-signed the letter access_token  
https.get ( 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid= your appid write here your secret & secret = write here', function (_res) {
                // this asynchronous callbacks where you can get the access_token 
          })

 

 Note: The front-end will request cross-domain requests, the best treatment is the background, you can not initiate a request by Postman

 

2, to obtain the required signatures ticket microcells

https.get ( 'acquired in the previous step https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token= type = & JSAPI the access_token', function (_res) {
          // this can be acquired in an asynchronous callback Ticket 
});

 Note: The front-end will request cross-domain requests, the best treatment is the background, you can not initiate a request by Postman

3, the specific method of generating a digital signature
ticket, noncestr, timestamp, url rule by micro message encrypted with sha1
noncestr and specific timestamp generating method Function in the official letter sample package
// noncestr
     var createNonceStr = function() {
          return Math.random().toString(36).substr(2, 15);
     };

      // timestamp
     var createTimeStamp = function () {
          return parseInt(new Date().getTime() / 1000) + '';
     };

I can also refer to the code in the code

// Calculation signature method 
     var calcSignature = function (Ticket, noncestr, TS, URL) {
           var STR = 'jsapi_ticket =' + Ticket + '& noncestr =' + noncestr + '& timestamp =' + TS + '& URL =' + URL; 
          shaObj = SHA1 (STR );
           return shaObj ; 
     } 
var Signature = calcSignature (Ticket, noncestr, timestamp, URL); 
Shai encrypted file
 import * as sha1 from 'sha1'
 hex_sha1.hex_sha1(str)
4, the output return signature used when a digital signature, and generate a digital signature timestamp, nonceStr, url, and appid
Because customers have to use in the initialization of the micro-channel JS-SDK
 
5, thus generating a digital signature success
 
wx.config ({ 
        Beta: to true , 
        Debug: false , // turn on debug mode, all calls will alert api return values out of the client, to view the incoming parameters, you can open the pc side, parameter information will by log play, it will be printed only when the pc. 
        appId: 'ww1437b9f5d1f742c6' , 
        timestamp: _this.timestamp, // required to generate a signature timestamp 
        nonceStr: _this.nonceStr, // required to generate a random signature string 
        signature: _this.signature, // required, signatures, see Appendix -JS-SDK usage rights signature algorithm 
        jsApiList: [
           'checkJsApi' ,
           'updateAppMessageShareData' ,
           'updateTimelineShareData' ,
           'onMenuShareAppMessage',  // old interface, i.e. waste 
          'onMenuShareTimeline' ,
           'shareWechatMessage' 
        ], 
        Success: function (RES) {
           // Callback 
          the console.log (RES) 

        }, 
        Fail: function (RES) { 
          the console.log (RES) 
          IF ( res.errMsg.indexOf ( 'Not exist function')> -1 ) { 
            Alert ( 'version upgrade low' ) 
          } 
        } 
      })

Guess you like

Origin www.cnblogs.com/chenzxl/p/12538438.html
Recommended