The method and precautions of WeChat public account calling scan

Call method:

  1. on the WeChat public platform
  2. Import JS file: <script src='https://res.wx.qq.com/open/js/jweixin-1.2.0.js'></script> 
  3. Inject permission verification configuration through the config interface ( very important ), handle successful verification through the ready interface, and handle failed verification through the error interface
  4. <script type="text/javascript">
             
            function scan(){
                 
                wx.config({
                    debug: true ,    // Whether to debug, true, a pop-up window will appear 
                    appId: ' <%=appID%> ' , // To get this thing, you must first have a public account, or an enterprise account, if you already have it, you can Know where the AppId is. 
                    timestamp: ' <%=time%> ' , // Generate signature timestamp, random number, and then use this to generate a signature 
                        nonceStr: ' <%=randstr%> ' , // Generate signature random string, random string , and then use this to generate a signature 
                    signature: ' <%=signstr%> ' , // this is the signature, in step 4, I will explain in detail 
                    jsApiList: [
                       'scanQRCode '  // Enable the scan function, you can add more functions here, such as WeChat payment 
                    ]
                });
                
                wx.ready(function () {
                    wx.scanQRCode({
                        desc: ' scanQRCode desc ' ,
                        needResult: 1 , // The default is 0, the scan result is processed by WeChat, 1 returns the scan result directly, 
                        scanType: [ " qrCode " , " barCode " ], // You can specify whether to scan a QR code or a 1D code, the default is 2 All have 
                        success: function (res) {
                             var url = res.resultStr;
                             var tempArray = url.split( ' , ' );
                             var tempNum = tempArray[ 1 ]; // This is the scan value returned after scanning 
                        }
                    });
                });
            }
             
            </script>
Matters needing attention: 1. wx.config, wx.ready, wx.scanQRCode, etc. are built-in interfaces of WeChat browser, which can only be recognized by WeChat browser. It cannot be debugged with IE on the computer, and an undefined error will be reported.

                       2. appId, timestamp, nonceStr are the APPID, timestamp and random number of the official account respectively, there should be no doubt about this

                                     The key is the signature algorithm, the steps are as follows:

                                     ① Obtain access_token based on APPID and APPSECRET:

                                           The interface URL is "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret

                                     ② Get jsapi_ticket according to access_token:

                                           The interface URL is "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + accesstocken + "& type=jsapi "

                                           Note: It must be type=jsapi. The reason why my signature was wrong at first was that it was written as type=wx_card (this is used to call the card and coupon interface, and it was copied and ignored at the time), which caused the signature I calculated to use signature verification. There is no problem with the verification tool, but calling the scan interface is to report a signature error, because this negligence wasted two days to find the reason!

                                     ③ Calculate the signature:

                string url = this .Request.Url.ToString();// Note: url should be dynamically obtained
                 string string1 = " jsapi_ticket= " + api_ticket + " &noncestr= " + randstr + " ×tamp= " + time + " &url= " + url + "" ;
                 // Encrypt this string with sha1 
                signature = FormsAuthentication.HashPasswordForStoringInConfigFile(string1, " SHA1 " );
                signature = signstr.ToLower();

Follow this step and you should be fine.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325300138&siteId=291194637