Home > Article > WeChat Applet > WeChat development to obtain access_token
access_token is the globally unique ticket of the public account. The public account needs to use access_token when calling each interface. Developers need to store it properly. At least 512 characters of space must be reserved for access_token storage. The validity period of access_token is currently 2 hours and needs to be refreshed regularly. Repeated acquisition will cause the last access_token to become invalid.
APIInstructions on the use and generation of the access_token required for calling the public platform:
1. In order to keep appsecrect confidential, the third party needs an access_token to obtain and Refreshed central control server. The access_token used by other business logic servers comes from the central control server and should not be refreshed individually, otherwise it will cause the access_token to be overwritten and affect the business;
2. The current validity period of the access_token is conveyed through the returned expire_in, which is currently Value within 7200 seconds. The central control server needs to refresh the new access_token in advance according to this valid time. During the refresh process, the central control server still outputs the old access_token. At this time, the public platform backend will ensure that both the old and new access_token are available within a short period of time during the refresh, which ensures the smooth transition of third-party businesses;
3. The validity time of access_token may be adjusted in the future, so the central control server not only needs to actively refresh internally at regular intervals, but also needs to provide an interface for passively refreshing access_token, so that the business server can trigger access_token when the API call learns that access_token has timed out. Refresh process.
If the third party does not use the central control server, but chooses each business logic point to refresh the access_token separately, conflicts may occur, leading to service instability.
The public account can use AppID and AppSecret to call this interface to obtain access_token. AppID and AppSecret can be obtained from the official website of WeChat Public Platform - Developer Center page (you need to have become a developer, and the account has no abnormal status). Note that the https protocol must be used when calling all WeChat interfaces.
Interface call request description
http request method: GET
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
Parameter description
Return instructions
Under normal circumstances, WeChat will return the following JSON data packet to the public account:
{"access_token":"ACCESS_TOKEN","expires_in":7200}
When an error occurs, WeChat will return error code and other information. The JSON data packet example is as follows (this example is an invalid AppID error):
{"errcode":40013,"errmsg":"invalid appid"}
[Related recommendations]
1. Special recommendation: "php Programmer Toolbox" V0.1 version download
2. WeChat public account platform source code download
3. Alizi order system source code download
The above is the detailed content of WeChat development to obtain access_token. For more information, please follow other related articles on the PHP Chinese website!