1. QR Code Authorization Login

[!note]

The app in the following process and the account 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.

If you do not know how to solve it, you need to contact the corresponding project manager of the company.

1.1. Flow Description

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:

sequenceDiagram participant APP participant Device with QR code participant Serve Device with QR code->>Serve: Get token ① Serve-->>Device with QR code: Return token Device with QR code->>Device with QR code: Generate QR code ② loop Auth status Device with QR code->>Serve: Get login status ③ Serve-->>Device with QR code: Return to login status end APP->>Device with QR code: Scan QR code: get token ④ Device with QR code-->>APP: Get token APP->>Serve: Authorization: use token ⑤ Serve-->>APP: Authorization success/failure alt Auth success Device with QR code->>Device with QR code: Go to homepage else end

Explanation of the key steps marked in the figure:

1.1.1. ①、Get token

The device requests the interface to obtain the token used in the authorization process, the interface is getQRCodeToken

1.1.2. ②、Generate QR code

Use the obtained token to generate a QR code in a specific format:

The format is: tuyaSmart--qrLogin?token=xxxxxxx

E.g:

tuyaSmart--qrLogin?token=AZc72de000-ec00-4000-9e51-b610fc300000

Generate QR code

二维码图片_6月28日11时52分25秒

Display the QR code generated from the above string on the screen of the device;

1.1.3. ③、Get login status

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

1.1.4. ④、Scan QR code

The app scans the QR code on the device, resolves the token in the QR code, and performs the authorization operation

1.1.5. ⑤、Authorization

Send the parsed QR code to the Server to complete the authorization action.

The authorization interface is QRcodeAuth

1.2. Interface Description

1.2.1. Get token interface

Used to obtain 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) {

    }
});

1.2.2. Get login status interface

Declaration

void QRCodeLogin(String countryCode, String token, ILoginCallback callback);

Parameters

Parameters Description
countryCode country code, for example: 86
token token
callback callback

示例代码

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) {

    }
});

1.2.3. Authorization interface

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) {

    }
});

results matching ""

    No results matching ""