To simplify the user login process, users can utilize the Quick Login feature for website applications in WeChat 3.9.11 for Windows and later, as well as WeChat 4.0.0 for Mac and later.
When a website application initiates a WeChat login request, if the user is already logged in to a qualifying WeChat client on their Windows / Mac device in an unlocked state, the system will prioritize prompting the user to perform a quick login using the account currently active in that WeChat client.
Quick Login does not require scanning a QR code; confirmation can be performed directly on the Windows / Mac device. Users can still switch to other WeChat accounts or log in via QR code.
Preparation
The WeChat login for web applications is a WeChat OAuth2.0 authorization login system built based on the OAuth2.0 protocol standard.
Before integrating WeChat OAuth2.0 authorization login, register a developer account on the WeChat Open Platform, ensure you have a web application that has passed the review, and obtain the corresponding AppID and AppSecret. Once you have applied for WeChat login and passed the review, you can proceed with the integration process.
Authorization Flow Description
WeChat OAuth2.0 authorization login allows WeChat users to securely log in to third-party applications or websites using their WeChat identity. After a WeChat user authorizes a third-party application integrated with WeChat OAuth2.0, the third party can obtain the user's API access credential (access_token). With the access_token, API calls can be made to the WeChat Open Platform authorization relationship interface, enabling the retrieval of basic user information and the implementation of basic open features for the user.
WeChat OAuth2.0 authorization login currently supports the authorization_code mode, which is suitable for application authorization with a server side. The overall flow for this mode is as follows:
- The third party initiates a WeChat authorization login request. Once the WeChat user allows authorization to the third-party application, WeChat launches the application or redirects to the third-party website, carrying the temporary authorization ticket code parameter;
- Using the code parameter along with the AppID and AppSecret, an API call is made to exchange for the access_token;
- The access_token is used for API calls to retrieve basic user data resources or to help users perform basic operations.
Sequence diagram for obtaining the access_token:
Step 1: Request CODE
Before using website application authorized login, third parties should ensure that the corresponding web authorization scope (scope=snsapi_login) has been obtained. You can then open the following link on the PC:
https://open.weixin.qq.com/connect/qrconnect?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
If the prompt "This link cannot be accessed" appears, please check if the parameters are filled in correctly. For example, the domain of redirect_uri must match the authorized domain filled in during review, or the scope must be snsapi_login.
Parameter Description
| Parameter | Required | Description |
|---|---|---|
| appid | Yes | The unique identifier of the application |
| redirect_uri | Yes | Please process the link using urlEncode |
| response_type | Yes | Fill in code |
| scope | Yes | The application authorization scope. If there are multiple scopes, separate them with commas (,). For web applications, only snsapi_login is currently supported |
| state | No | Used to maintain the state of the request and callback. It is returned to the third party as is after the authorization request. This parameter can be used to prevent CSRF attacks. It is recommended that third parties include this parameter, which can be set to a simple random number plus session for verification |
| lang | No | Interface language. Supports cn (Simplified Chinese) and en (English). The default is cn |
Response Description
After the user allows authorization, they will be redirected to the URL specified by redirect_uri with the code and state parameters included:
redirect_uri?code=CODE&state=STATE
If the user denies authorization, no redirection will occur.
Embedding WeChat Login QR Code in Your Own Page
To meet more customized needs for websites, an embedded QR code login method is also provided. After users scan the QR code with WeChat to authorize, the code is returned to the website via JS.
Step 1: Include the following JS file in the page (supports https):
http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js
Step 2: Instantiate the JS object where WeChat login is required:
var obj = new WxLogin({
self_redirect: true,
id: "login_container",
appid: "",
scope: "",
redirect_uri: "",
state: "",
style: "",
href: "",
onReady: function(isReady) {
console.log(isReady);
}
});
Parameter Description
| Parameter | Required | Description |
|---|---|---|
| self_redirect | No | true: After the phone confirms login, it can redirect to redirect_uri within the iframe. false: After the phone confirms login, it can redirect to redirect_uri in the top window. The default is false. |
| id | Yes | The container id on the third-party page to display the QR code |
| appid | Yes | The unique identifier of the application, obtained after the application review on the WeChat Open Platform is approved |
| scope | Yes | The application authorization scope. If there are multiple scopes, separate them with commas (,). For web applications, only snsapi_login is currently supported |
| redirect_uri | Yes | The redirect URL. UrlEncode is required |
| state | No | Used to maintain the state of the request and callback. It is returned to the third party as is after the authorization request. This parameter can be used to prevent CSRF attacks |
| style | No | Offers "black" and "white" options. The default is black text description. See FAQ for details |
| href | No | Custom style link. Third parties can override the default style according to actual needs |
| stylelite | No | Switches the QR code login style. The value 1 switches to the new style. See FAQ for details |
| fast_login | No | Enables or disables the fast login feature. The value 0 disables fast login |
| color_scheme | No | Supports switching between light or dark themes. The value "auto" indicates following the system theme |
| onReady | No | Callback for whether the iframe page has loaded successfully |
| onQRcodeReady | No | Callback for when the QR code loading is complete |
Step 2: Obtain access_token via code
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
Parameter Description
| Parameter | Required | Description |
|---|---|---|
| appid | Yes | The unique identifier of the app, obtained after the app passes the review on the WeChat Open Platform |
| secret | Yes | The app secret (AppSecret), obtained after the app passes the review on the WeChat Open Platform |
| code | Yes | The code parameter obtained in Step 1 |
| grant_type | Yes | Set to authorization_code |
Response Description
Successful response:
{
"access_token": "ACCESS_TOKEN",
"expires_in": 7200,
"refresh_token": "REFRESH_TOKEN",
"openid": "OPENID",
"scope": "SCOPE",
"unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
}
Step 3: Call APIs Using access_token
After obtaining the access_token, you can proceed with API calls under the following prerequisites:
- The access_token is valid and has not expired;
- The WeChat user has authorized the third-party application account for the corresponding API scopes (scope).
For details on API call methods, refer to 《WeChat Authorization Relationship API Calling Guide》.
FAQ
1. What is the authorization temporary ticket (code)?
Answer: It is required for third parties to obtain the access_token using the code. The code expires after 10 minutes, and one code can only be successfully exchanged for one access_token, after which it becomes invalid. The temporary and single-use nature of the code ensures the security of WeChat authorized login. Third parties can further enhance the security of their authorized login by using HTTPS and the state parameter.
2. What is the authorization scope (scope)?
Answer: The authorization scope (scope) represents the API permissions that the user authorizes to the third party. The third-party application must apply to the WeChat Open Platform for permission to use the corresponding scope. After the user grants authorization via the method described in the documentation and the corresponding access_token is obtained, the API can be invoked.
3. What is the role of the style field in the JS code for embedded QR code WeChat login on websites?
Answer: Third-party page color schemes may be light or dark. If the third-party page has a light background, the style field should provide the value "black" (or be omitted, as black is the default), which makes the corresponding WeChat login text style black. The related effect is as follows:
If the value "white" is provided, the corresponding text description will be displayed in white, which is suitable for dark backgrounds. The related effect is as follows:
4. What is the role of the href field in the JS code for embedded QR code WeChat login on websites?
Answer: If third parties find that the default style provided by the WeChat team does not match their page style, they can provide their own style files to override the default style. For example, if a third party feels that the default QR code is too large, they can provide the relevant CSS style file and fill the link address into the href field:
.impowerBox .qrcode {width: 200px;}
.impowerBox .title {display: none;}
.impowerBox .info {width: 200px;}
.status_icon {display: none}
.impowerBox .status {text-align: center;}
The related effect is as follows:
5. What is the role of the stylelite field in the JS code for embedded QR code WeChat login on websites?
Answer: We have revamped the UI for WeChat authorized login. To ensure that the existing WeChat QR code login functionality is not affected, the stylelite parameter is provided to allow choosing between the old and new UIs. By default, it is the old UI. When stylelite is set to 1, it switches to the new UI, and any custom styles introduced via the href parameter will become invalid. To ensure proper display, it is recommended to reserve at least 220px * 220px for the QR code scanning area.
The effect of the new QR code style is as follows: