Note: The app in the following process and the accounting system of the central control device need to be associated to be used, otherwise, an error will be reported.
For example, if you use the Tuya Smart app to scan the QR code of your SDK created by the IoT platform, an error will be reported and the session will be invalid.
The QR code authorization login function is suitable for APP scanning code to authorize another device to log in to the same account. The device can be a central control device, TV, Pad, etc. The complete authorization process is as follows:
Explanation of the key steps marked in the figure:
The device requests the interface to obtain the token used in the authorization process, the interface is getQRCodeToken
Use the obtained token to generate a QR code in a specific format:
The format is: tuyaSmart--qrLogin?token=xxxxxxx
Example:
tuyaSmart--qrLogin?token=AZc72de000-ec00-4000-9e51-b610fc300000
Generate QR code
Display the QR code generated from the above string on the screen of the device;
Obtain whether the authorization is successful from the server. If the authorization is successful, the user information will be returned, jump to the application home page, and enter the subsequent operation.
The interface is: QRCodeLogin
The app scans the QR code on the device, resolves the token in the QR code, and performs the authorization operation
Send the parsed QR code to the Server to complete the authorization action.
The authorization interface is QRcodeAuth
Used to obtain the token
Declaration
void getQRCodeToken(String countryCode, IGetQRCodeTokenCallback callback);
Parameters
Parameters | Description |
---|---|
countryCode | country code, for example: 86 |
callback | callback |
Example
TuyaHomeSdk.getUserInstance().getQRCodeToken("86", new IGetQRCodeTokenCallback() {
@Override
public void onSuccess(String token) {
}
@Override
public void onError(String code, String error) {
}
});
Declaration
void QRCodeLogin(String countryCode, String token, ILoginCallback callback);
Parameters
Parameters | Description |
---|---|
countryCode | country code, for example: 86 |
token | token |
callback | callback |
Code sample
TuyaHomeSdk.getUserInstance().QRCodeLogin("86", "xxxx", new ILoginCallback() {
@Override
public void onSuccess(User user) {
if (user != null && !TextUtils.isEmpty(user.getSid())){
TuyaHomeSdk.getUserInstance().loginSuccess(user);
//get homeId
Object homeId = user.getExtras().get("homeId");
gotoHomePage();
}
}
@Override
public void onError(String code, String error) {
}
});
Declaration
void QRcodeAuth(String countryCode, long homeId, String token, IBooleanCallback callback);
Parameters
Parameters | Description |
---|---|
countryCode | country code, for example: 86 |
homeId | home id. Please refer to the Home Management chapters |
token | token |
callback | callback |
Example
TuyaHomeSdk.getUserInstance().QRcodeAuth("86", mHomeId, getActivityToken(), new IBooleanCallback() {
@Override
public void onSuccess() {
}
@Override
public void onError(String code, String error) {
}
});