# In exchange for user authorization credentials

Debugging Tools

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 NametypeRequired to fill inExampleIntroductions
appidstringyeswx520c15f417810387Unique identifier for Official Account
secretstringyes-Official Account of appsecret
codestringyes-Fill in the code parameters obtained by the
grant_typestringyesauthorization_codeEnter as authorization_code

# Request BodyRequest Payload

nothing

# 3. Return Parameters

# Response Payload

Parameter NametypeIntroductions
access_tokenstringWeb page authorization interface invocation credentials. Note: This access_token is different from the underlying supported access_token
expires_innumberaccess_token Interface Call Credential Timeout, in seconds
refresh_tokenstringUser refresh access_token
openidstring[Official Account Official Account Message Template: [Official Account]
unionidstringUser 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_snapshotusernumberWhether 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

  1. WeChat Web page authorization is achieved throughOAuth2.0mechanism,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.
  2. 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

  1. Takesnsapi_baseasscopeThe initiated web authorization is used to obtain theopenidof 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).
  2. A web authorization initiated withsnsapi_userinfoforscopis 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.
  3. 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

  1. Please note that web page authorization to access user basic information also follows theUnionIDmechanism.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.
  2. UnionIDIf 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 codeError DescriptionSolutions
40029invalid codeInvalid 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;