This API should be called on the server side and cannot be called directly on the client side (Mini Program, web page, APP, etc.). For details, please refer to API Call Guide.
API English Name: getAccessToken
This interface is used to obtain a globally unique background API call credential (Access Token). The token is valid for 7,200 seconds. Developers should keep it securely. For usage guidelines, please refer to this document.
It is recommended to use obtaining a stable version of API call credentials.
- If using Cloud Development, you can use cloud calls for maintenance-free access_token calls.
- If using Cloud Hosting, you can also use WeChat Token/Open API Service for maintenance-free access_token calls.
1. Call Method
HTTPS Call
GET https://api.weixin.qq.com/cgi-bin/token
Cloud Call
- This API does not support cloud call.
Third-party Call
- This API does not support third-party platform calls.
2. Request Parameters
Query Parameters Query String Parameters
None
Request Body Request Payload
| Parameter | Type | Required | Description |
|---|---|---|---|
| grant_type | string | Yes | Enter client_credential |
| appid | string | Yes | The unique credential for the account, namely AppID. Click here to view how to obtain AppID. |
| secret | string | Yes | The unique credential key, namely AppSecret. Click here to view how to obtain AppSecret. |
3. Response Parameters
Response Body Response Payload
| Parameter | Type | Description |
|---|---|---|
| access_token | string | The obtained credentials |
| expires_in | number | Credential validity period, in seconds. Currently, it must be within 7,200 seconds. |
4. Notes
- The
Access Tokensfor different application types are isolated from each other and can only be used to call interfaces specific to their respective application types. AppSecretis the key used by the account to access backgroundAPIinterfaces. Developers should keep it securely to prevent account misuse due to leakage.- If there is no need to use
AppSecretfor an extended period, developers can freeze it to enhance account security. - Once
AppSecretis frozen, developers cannot use it to obtainAccess Token(the interface will return error code 40243). This does not affect the normal use of basic account functions, third-party authorized background interface calls, or cloud development background interface calls. - Developers can unfreeze
AppSecretat any time.
For information on how to obtain AppID and AppSecret, as well as how to freeze/unfreeze AppSecret, please refer to this document.
5. Code Example
Request Example
GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
Response Example
{
"access_token": "ACCESS_TOKEN",
"expires_in": 7200
}
6. Error Codes
The following is the error code list of this API, other error codes can refer to Common Error Codes; When encountering errors in calling the API, you can use the official API Diagnostic Tool to assist in locating and analyzing problems.
| Error Code | Error Description | Solution |
|---|---|---|
| -1 | system error | The system is busy. Please try again later. |
| 40001 | invalid credential access_token isinvalid or not latest | An error occurred with AppSecret when obtaining access_token, or the access_token is invalid. Please verify the correctness of AppSecret or ensure that you are calling the interface for the correct public account. |
| 40002 | invalid grant_type | The credential type is invalid. |
| 40013 | invalid appid | The AppID is invalid. Please check its correctness, avoiding special characters and paying attention to case sensitivity. |
| 40125 | The secret is invalid. | Please verify the correctness of the secret, avoiding special characters and paying attention to case sensitivity. |
| 40164 | The IP address used to call the interface is not in the allowlist. | Please add the IP address to the interface allowlist. |
| 40243 | AppSecret has been frozen. Please unfreeze it before making another call. | Click here to view how to unfreeze AppSecret. |
| 41004 | appsecret missing | The secret parameter is missing. |
| 50004 | Using the token interface is prohibited. | |
| 50007 | The account has been frozen. |
7. Applicable Scope
| Mini Program | Official Account | Service Account | Mini Game | WeChat Shop | Alliance Agency | Influencer | Shop Supplier | Mobile App | Website App | Channels Window | Multi-platform App |
|---|---|---|---|---|---|---|---|---|---|---|---|
| ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ |
- ✔:This account type can call this API。
- Other account types not explicitly declared cannot call this API unless otherwise specified.