Loading…

access_token invalidation and how to solve it

When calling the WeChat official account/mini program related interface, if the following error is returned, it means that the current access_token is wrong: ```json { "errcode": 40001, "errmsg": "invalid credential, access_token is invalid or not latest rid: 5f38a263-3d4c3ccd-01e705f7" } ``` It is easy to understand the cause of this error based on the above literal prompt, or you can also check the description of the 40001 error code in WeChat's official development documentation. The above situation usually occurs for the following two reasons. Please investigate one by one: ## The secret corresponding to appID has changed The access_token saved by "apifm" is obtained through the appID and secret you configured in the background, so if an access_token error is prompted, the first step is to check whether the secret configured in the background has been reset by someone else in the background of the mini program's official account. After the reset, no new secret is configured in the background of "apifm", causing "apifm" to still use the old secret to obtain the access_token, causing the acquisition to continue to fail. You can go to the "apifm" backend, go to the left menu "WeChat Settings" --> "Mini Program Settings" / "Official Account Settings", click the "Configure Appid/Secret" button, and click the "OK" button in the pop-up window without modifying anything. If it prompts that the access_token cannot be obtained, then you can confirm that the secret has been modified and fill in the correct secret to solve the problem. If no error message is prompted when you click the "OK" button, then it can basically be ruled out that the secret has been reset. Then please look down for another reason. ## You can obtain access_token elsewhere WeChat's access_token can only be obtained in one place. If you obtain the access_token through the appID and secret in another place (other than the "apifm"), then the access_token on the apifm will become invalid immediately. In the same way, if the "apifm" obtains the access_token through the appID and secret, the access_token you obtained elsewhere will also become invalid immediately. So please confirm whether you are using appID and secret in multiple places to obtain access_token. If so, then you only need to ensure that you obtain access_token in the same place to solve the problem. **But I have other businesses of my own that also need to use**`**access_token**`**, how should it be reused?** The apifm will refresh `access_token` before the validity period is about to expire according to the requirements of WeChat, so you can directly call the backend api interface of the apifm, directly read the currently valid `access_token` and use it directly, because the validity period of `access_token` specified by WeChat is 2 hours, so you need to pull it regularly by yourself. At the same time, you need to get a new one on your own business end if you find that `access_token` is invalid. Interface address: https://common.apifm.com/apifmUser/wxMiniprogramSet/list POST method call The request header needs to add `X-TOKEN` or `Authorization` If you are in a secure and confidential environment such as your own server, your own computer, cloud development, etc. (**Non-safe environments such as small programs and H5 front-ends**), it is recommended to use the `Authorization` method: ["Basic Authentication calls the background interface without login"](https://www.yuque.com/apifm/doc/guzeid)