Official WeChat Authorization Interface

The WeChat web page authorization login interface, used to implement user authorization login in the WeChat environment, supporting the acquisition of user basic information and the issuance of verification codes.

Points to note

  • This interface uses the GET method and is a redirection interface
  • It needs to be accessed in the built-in WeChat browser to work properly
  • It will first redirect to the official WeChat authorization page, and return after the user authorizes
  • When the state parameter is code, a verification code will be sent to the user's WeChat
  • The public WeChat official account authorization callback domain must be configured in advance

Request address

{域名地址}/api/wechat/auth

Description:{域名地址}Replace it with your domain name, such ashttps://en.anqicms.com/api/wechat/auth

Request Syntax

GET {域名地址}/api/wechat/auth

Request Headers

This interface also involves common request headers. For more information, please refer toCommon Request Headers (Common Request Headers).

Request parameters

Field name Type Required Description
State string No Status parameter, when the value iscodeWhen, the verification code will be sent to the WeChat official account window, other values have no special function

Response header

This interface only involves common response headers. For more information, please refer toCommon Request Headers (Common Request Headers).

Return parameters

This interface will jump to the WeChat authorization page, and a pop-up result will be displayed on the front end after authorization is completed:

  • Normal situation: Display authorization success message
  • When state=code: Send verification code to user's WeChat and display prompt information
  • Error situation: Display error message pop-up

Usage Example

Normal Authorization Request Example

GET /api/wechat/auth?state=default HTTP/1.1
Host: www.anqicms.com
User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) MicroMessenger/8.0.0

Get Verification Code Request Example

GET /api/wechat/auth?state=code HTTP/1.1
Host: www.anqicms.com
User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) MicroMessenger/8.0.0

Authorization Success Response Example

HTTP/1.1 302 Found
Location: https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect

Example of error response

{
  "code": -1,
  "msg": "微信授权失败"
}

error code

Error code Description
0 Authorization Success
-1 Authorization Failed, Error Reason in msg
1001 Not logged in
1002 Not authorized
200 API request OK
4001 Not opened in WeChat
4002 WeChat authorization parameter error