Signature acquisition based on wechat signature (example explanation)

The signature algorithm shared by wechat is also written on wechat, The main reason is that the calling interface needs to use the server (that's what the official wechat document says. It's OK to try the front end). However, the access_token and jsapi_ticket of wechat have a limit on the number of times of use, so we still use the server to obtain them, save them after they are obtained, and retrieve them again after the next use judgment timeout. This is enough. Otherwise, it will be embarrassing that the number of interface calls exceeds the limit Things have changed.

If you need to use a custom sharing document, the service number or subscription number must be authenticated (my subscription number is a personal type and cannot be authenticated, so the sharing function cannot be used)

I use node as the background, so the code uses JS code. Of course, other code can also be used. The logic is the same, and the code writing method is different.

1., first, the official account.

What I applied for here is a subscription number

First, you must obtain your own developer ID (appid) and developer password (appsecret) under development - > basic configuration

Then set the IP address of the server in the IP white list option under the same directory

In this way, the basic server settings are completed.

2. Then there is what we are best at - writing code

According to the official wechat documents, in the first step, we need to get access_ Token, and this access_ The token has a validity period of 7200 seconds, so you get access_ The token should be stored locally in the future (file storage or database storage is OK, just save it anyway)

The specific implementation code is as follows

First, we need to introduce the module corresponding to the node (the mongodb database needs to be started every time it is used. I don't think it's troublesome, so I use file storage here)

From top to bottom

The express module is used to create a server to interface with the front-end and wechat respectively (it seems to be of little use here, but you can use the HTTP module instead)

The HTTPS module is a module used to send HTTPS requests (wechat requests need to use HTTPS requests, but HTTP cannot)

FS module file operation module. If the database is used, it needs to be replaced with the corresponding module

Crypto module, encryption module, wechat signature algorithm needs to be encrypted by SHA1 algorithm, as mentioned below

All modules are introduced. Next, some easy-to-use methods are defined

First, to start a server:

Then define how to read and write files

Then use to define a method for sending HTTPS requests

With the basic methods, you can request the wechat interface

The above method is to obtain wechat token. I first read it from the local file, and then call the interface if I can't read it (I'm just testing and using it without judgment. In actual operation, I need to judge the timestamp. If the access_token expires, I need to delete the content in the file and request a new access_token again)

access_ With the token, the next step is to get the jsapi_ ticket:

jsapi_ The acquisition and storage logic of ticket and token are the same

The next step is signature generation

After the signature is generated, the signature, random string, appid and timestamp are simultaneously transmitted to the front end through res.send:

After receiving data, the front end calls wx.. Config() and pass in the drink parameters to get the corresponding wechat JS permissions.

The above article based on wechat signature acquisition (example explanation) is all the content shared by Xiaobian. I hope it can give you a reference and support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>