This article introduces how to use the WeChat QR code login feature in mobile apps, supporting iOS, Android, and HarmonyOS.
Overview
The QR Code Login capability allows developers to retrieve a QR code within a mobile application. Users can scan this QR code using the WeChat client to log in to the mobile application. This capability can be applied to scenarios such as multi-device login, smart hardware, and TV boxes.
iOS QR Code Scanning Authorization to Obtain Code Flow
Step 1: Request function
(BOOL)Auth:(NSString *)appId nonceStr:(NSString *)nonceStr timeStamp:(NSString*)timeStamp scope:(NSString *) scope signature:(NSString *)signature schemeData:(NSString *)schemeData;
Parameter Description
| Parameter | Required | Description |
|---|---|---|
| appid | Yes | Unique identifier of the application |
| scope | Yes | Scope of application authorization. If there are multiple scopes, separate them with commas (,). These are the scopes possessed by the App |
| nonceStr | Yes | A random string that is as unique as possible, used to ensure that each signature is different |
| timeStamp | Yes | Timestamp |
| signature | Yes | Signature |
| schemeData | No | Appended after the scheme following the QR code scan |
Step 2: Listen for the QR code callback and display the QR code when the callback is triggered
-(void)onAuthGotQrcode:(UIImage *)image; // Get the QR code
Step 3: Callback authCode after the user confirms login
-(void)onAuthFinish:(int)errCode AuthCode:(NSString *)authCode; // Login successful
Android Scan Code Authorization Flow
Flow
First, the App initiates authorization via the IDiffDevOAuth.auth() interface. Then, it obtains the QR code through the OAuthListener.onAuthGotQrcode() callback, displays the QR code within the App, and finally, the user scans the QR code with WeChat to authorize.
Interface
IDiffDevOAuth
boolean auth(String appId, String scope, String noncestr, String timestamp, String signature, OAuthListener listener)
Parameter Description
| Parameter | Required | Description |
|---|---|---|
| appId | Yes | Unique identifier of the app |
| scope | Yes | Authorization scope of the app. If multiple scopes exist, separate them with commas (,). This represents the scopes possessed by the App |
| noncestr | Yes | A random string intended to be as non-repetitive as possible, used to ensure each signature is unique |
| timestamp | Yes | Timestamp |
| signature | Yes | Signature |
| listener | Yes | Callback interface for the authorization flow |
OAuthListener
/**
* Interface returning the QR code after auth
*
* @param qrcodeImgPath Deprecated
* @param imgBuf QR code image data
*/
void onAuthGotQrcode(String qrcodeImgPath, byte[] imgBuf);
/**
* Callback invoked after the user scans the QR code
*/
void onQrcodeScanned();
/**
* Callback invoked after the user clicks to authorize
*/
void onAuthFinish(OAuthErrCode errCode, String authCode);
HarmonyOS Scan-based Authorization Flow
Flow
First, the App initiates authorization using the IDiffDevOAuth.startOAuth() interface. Then, it retrieves the QR code via the OAuthCallback.onGotQRCode() callback interface and displays the QR code in the App. Finally, the user scans the QR code with WeChat to complete authorization.
Interfaces
IDiffDevOAuth
startOAuth(appId: string, scope: string, noncestr: string, timestamp: string, signature: string, callback: OAuthCallback): void
Parameters
| Parameter | Required | Description |
|---|---|---|
| appId | Yes | The unique identifier of the application |
| scope | Yes | The authorization scopes of the application. If multiple scopes are present, separate them with commas (,). These are the scopes possessed by the App |
| noncestr | Yes | A random, non-repeating string used to ensure that each signature is unique |
| timestamp | Yes | The timestamp |
| signature | Yes | The signature |
| callback | Yes | The callback interface for the authorization flow |
OAuthCallback
/**
* The interface to return the QR code after authentication
*
* @param base64JpegImageBuffer QR code image data
*/
onGotQRCode: (base64JpegImageBuffer: string) => void
/**
* Callback interface invoked after the user scans the QR code
*/
onQRCodeScanned: () => void
/**
* Callback interface invoked after the user clicks authorize
*/
onAuthFinish: (authCode: string) => void
/**
* Callback interface invoked if an error occurs in the authorization flow
*/
onAuthError: (errCode: OAuthErrCode, errMsg: string) => void
SDK QR Code Login Signature Algorithm
Get Ticket
You must obtain the corresponding sdk_ticket before generating the signature.
The sdk_ticket is a temporary credential used for generating signatures. Under normal circumstances, the validity period of an sdk_ticket is 7200 seconds, and it is obtained via the access_token. Due to the limited number of API calls available for retrieving the sdk_ticket, frequent refreshing will result in API rate limiting, which may affect your own business. Developers are required to store and update the sdk_ticket in their own server-side storage.
- Refer to the documentation below to use the App's AppID and AppSecret to Obtain access_token
- Use the access_token obtained in the first step to request the sdk_ticket via an HTTP GET request:
https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=2
A successful response returns the following JSON:
{
"errcode": 0,
"errmsg": "ok",
"ticket": "-p3A5zVP95IuafPhzA6lRR95_F9nZEBfJ_n4E9t8ZFWKJTDPOwccVQhHCwDBmvLkayF_jh-m9HOExhumOziDWA",
"expires_in": 7200
}
Once the sdk_ticket is obtained, you can generate the signature for QR code login.
Signature Generation
The rules for generating the signature are as follows:
The fields included in the signature are the third-party appid, noncestr (a random string), a valid sdk_ticket, and timestamp (the Unix timestamp).
Sort all parameters to be signed by their field names (keys) in ascending ASCII order (lexicographic order), and concatenate them into the string string1 using the URL key-value pair format (i.e., key1=value1&key2=value2…). Note that all parameter names must be lowercase. Apply SHA-1 encryption to string1, using the original values for both field names and field values without URL encoding. In other words, signature = sha1(string1).
Example:
appid = wxappid
sdk_ticket = -p3A5zVP95IuafPhzA6lRR95_F9nZEBfJ_n4E9t8ZFWKJTDPOwccVQhHCwDBmvLkayF_jh-m9HOExhumOziDWA
noncestr = noncestr
timestamp = 1417508194
-
Sort all parameters to be signed by their field names (keys) in ascending ASCII order (lexicographic order): appid, noncestr, sdk_ticket, timestamp
-
Concatenate them into the string string1 using the URL key-value pair format:
appid=wxappid&noncestr=noncestr&sdk_ticket=-p3A5zVP95IuafPhzA6lRR95_F9nZEBfJ_n4E9t8ZFWKJTDPOwccVQhHCwDBmvLkayF_jh-m9HOExhumOziDWA×tamp=1417508194
- Apply SHA-1 signing to string1 to obtain the signature:
429eaaa13fd71efbc3fd344d0a9a9126835e7303