# In exchange for user authorization credentials
Interface should be called on the server side, not in the front end (Weixin Mini Program, web pages, APP, etc.) directly called, specific reference interface call guide
Interface name: snsAccessToken
access_token access_token
This interface is different from obtaining the interface invocation credentials , which is obtained for a user-level authorization token.The token can only get information about the authorized user and cannot be used to call other interfaces.
Note: The frequency limit for this interface is 50,000 / min
# 1. How to call
# HTTPS calls
GET https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=GRANT_TYPE
# Cloud Calls
- This interface does not support cloud calls
# Third party invocation
- Third Party Platform calls are not supported by this interface.
# 2. Request parameters
# Query parametersQuery String parameters
Parameter Name | type | Required to fill in | Example | Introductions |
---|---|---|---|---|
appid | string | yes | wx520c15f417810387 | Unique identifier for Official Account |
secret | string | yes | - | Official Account of appsecret |
code | string | yes | - | Fill in the code parameters obtained by the |
grant_type | string | yes | authorization_code | Enter as authorization_code |
# Request BodyRequest Payload
nothing
# 3. Return Parameters
# Response Payload
Parameter Name | type | Introductions |
---|---|---|
access_token | string | Web page authorization interface invocation credentials. Note: This access_token is different from the underlying supported access_token |
expires_in | number | access_token Interface Call Credential Timeout, in seconds |
refresh_token | string | User refresh access_token |
openid | string | [Official Account Official Account Message Template: [Official Account] |
unionid | string | User unionid (for an application under a WeChat open platform account, the same user's unionid is unique), only returned if the scope is "snsapi_userinfo" |
is_snapshotuser | number | Whether it is a snapshot page mode virtual account is returned only when the user is a Snapshot page mode Virtual Account. The value is 1 |
# 4. Note
# The difference between access_token and access_token
- WeChat Web page authorization is achieved through
OAuth2.0
mechanism,After the user is authorized to Official Account, the Official Account message template can obtain an interface call certificate (access_token) which is unique to the page authorization. After the user is authorized to Official Account, the Official Account message template can obtain the user's basic information. - Other WeChat interfaces require a normal access_token call to be obtained through the Get Interface Invocation Credentials interface in basic support.
# The difference between the two scopes of web page authorization
- Take
snsapi_base
asscope
The initiated web authorization is used to obtain theopenid
of the user who enters the page, and is silently authorized** and automatically jumps to the callback page.The user's perception is that they are directly on the callback page (often the business page). - A web authorization initiated with
snsapi_userinfo
forscop
is used to obtain basic user information.However, this authorization requires the user's manual consent, and since the user has consented, it is without concern that the user' basic information can be obtained after the authorization. - The User Management Interface's "Access to User Basic Information Interface" retrieves user basic information based on the user OpenID once the user and Official Account have a message interaction or follow-up event pushed.This interface, as well as other WeChat interfaces, requires the user (i.e. openid) to pay attention to the Official Account message template.
# UnionID mechanism
- Please note that web page authorization to access user basic information also follows the
UnionID
mechanism.If the developer has a need to harmonize user accounts between multiple Official Account, or between Official Account messages, Mobile App,The Weixin Open Platform (open.weixin.qq.com) is required to bind the Official Account message template before theUnion ID [
mechanism can be used to meet the above requirements. UnionID
If the developer has multiple Mobile App, Website App and public accounts,You can distinguish the uniqueness of a user by obtaining the unionid in the user's basic information, because the unionid is the same for different applications (mobile, web, and public accounts) under the same WeChat open platform.
# 5. Code examples
Example Requests
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_cod
Return an example
{
"access_token": "ACCESS_TOKEN",
"expires_in": 7200,
"refresh_token": "REFRESH_TOKEN",
"openid": "OPENID",
"unionid": "UNIONID",
"is_snapshotuser": 1
}
# 6. Error code
The following is a list of error codes for this interface, other error codes can refer to General error codes
Error code | Error Description | Solutions |
---|---|---|
40029 | invalid code | Invalid code parameter |
# 7. Scope of application
How this interface can be invoked under different account types:
Service Account | Mobile App | Website App |
---|---|---|
Certification only | ✔ | ✔ |
- Authentication only: means that only authenticated accounts are allowed to be invoked by the enterprise entity, and accounts that are not authenticated or do not support authentication cannot be invoked.
- ✔: The account can call this interface
- Other account types that are not expressly stated may not be called on this interface without special instructions;