We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All

Documents from this version have been archived, and will not continue to be maintained. Please use the latest version.

HMS Core GuidesSafety DetectUserDetect API

Safety Detect - UserDetect API

1. Service Features

    1. Fake user identification based on the real-time risk analysis engine: The API collects related data in the authorized scope and upload it to the real-time risk analysis engine to check whether the current user is a fake one.

    2. Fake user verification based on verification codes: If a user is a suspicious one or risky one, a verification code is sent to the user for secondary verification.


Video Player is loading.
Current Time 
Loaded: 0%
Duration 
1x

2. Use Cases

This API can help your app prevent batch registration, credential stuffing attacks, activity bonus hunting, and content crawling.

3. Architecture

(1) Your app integrates the HMS SDK to call HMS Core Safety Detect.

(2) Safety Detect evaluates risks in the device environment. If the risk level is medium or high, Safety Detect requires captcha and returns a response token to your app.

(3) Your app sends the response token to the app server.

(4) The app server sends the response token to the Safety Detect server to request the detection results.

4. Development Procedure

For details, please refer to Development Procedure.

5. Developing an App

5.1 Calling the UserDetect API

To call the UserDetect API, perform the following steps:

    1. Call the userDetection() method of the UserDetect API to initiate a fake user detection request. The API will return a responsetoken.

    2. Send the response token to your app server, and call the cloud API of HMS Core on your server to obtain the fake user detection result.

5.2 Initializing UserDetect

The UserDetect API provides the behavior detection capability. To use the capability, you can call the initUserDetect() API to initialize fake user detection. The sample code is as follows:

Collapse
Word wrap
Dark theme
Copy code
  1. // Replace related parameters with your activity or context.
  2. SafetyDetectClient client = SafetyDetect.getClient(MainActivity.this);
  3. client.initUserDetect().addOnSuccessListener(new OnSuccessListener<Void>() {
  4.     @Override
  5.     public void onSuccess(Void v) {
  6.         // Communication with the service was successful.
  7.     }
  8. }).addOnFailureListener(new OnFailureListener() {
  9.     @Override
  10.     public void onFailure(Exception e) {
  11.         // An error occurred during communication with the service.
  12.     }
  13. });

5.3 Initiating a Detection Request

To initiate a detection request, you need to call the userDetection method. Generally, this method is triggered when a user touches a UI control (such as a button). To call the userDetection method, perform the following steps:

    1. Transfer the applied appId as the input parameter of the method.

    2. Add the OnSuccessListener and OnFailureListener instances as listeners.

    3. Override onSuccess and onFailure to process the result.

The sample code for calling this method is as follows:

Collapse
Word wrap
Dark theme
Copy code
  1. public void onClick(View v) {
  2.    SafetyDetectClient client = SafetyDetect.getClient(getActivity());
  3.    String appId = "your_app_id";
  4.    client.userDetection(appId)
  5.            .addOnSuccessListener(new OnSuccessListener<UserDetectResponse>() {
  6.                @Override
  7.                public void onSuccess(UserDetectResponse userDetectResponse) {
  8.                    // Indicates communication with the service was successful.
  9.                    String responseToken = userDetectResponse.getResponseToken();
  10.                    if (!responseToken.isEmpty()) {
  11.                        // Send the response token to your app server, and call the cloud API of HMS Core on your server to obtain the fake user detection result.
  12.                    }
  13.                }
  14.            })
  15.            .addOnFailureListener(new OnFailureListener() {
  16.                @Override
  17.                public void onFailure(Exception e) {
  18.                    // There was an error communicating with the service.
  19.                    String errorMsg;
  20.                    if (e instanceof ApiException) {
  21.                        // An error with the HMS API contains some additional details.
  22.                        // You can use the apiException.getStatusCode() method to get the status code.
  23.                        ApiException apiException = (ApiException) e;
  24.                        errorMsg = SafetyDetectStatusCodes.getStatusCodeString(apiException.getStatusCode()) + ": "
  25.                                + apiException.getMessage();
  26.                    } else {
  27.                        // Unknown type of error has occurred.
  28.                        errorMsg = e.getMessage();
  29.                    }
  30.                    Log.i(TAG, "User detection fail. Error info: " + errorMsg);
  31.                }
  32.            });
  33. }

If the onSuccess() method is executed in the UserDetect API, the user has completed human-machine identification detection. However, you still need to verify the response token of the user on the background server.

5.4 Obtaining the Detection Result

Perform the following steps on the server:

    1. Obtain an access token.

    2. Call the cloud-side API to obtain the detection result.

The procedure is as follows:

Obtain an access token. For details, please refer to Open Platform Authentication.

Call the cloud-side API to obtain the detection result. The following is a request example: For details about the Server API, please refer to the Server API reference.

Collapse
Word wrap
Dark theme
Copy code
  1. POST https://hirms.cloud.huawei.com/rms/v1/userRisks/verify?appId=123456 HTTP/1.1
  2. Content-Type: application/json;charset=utf-8
Collapse
Word wrap
Dark theme
Copy code
  1. {
  2.    "accessToken":"AAWWHI94sgUR2RU5_P1ZptUiwLq7W8XWJO2LxaAPuXw4_HOJFXnBlN-q5_3bwlxVW_SHeDPx_s5bWW-9DjtWZsvcm9CwXe1FHJg0u-D2pcQPcb3sTxDTJeiwEb9WBPl_9w",
  3.    "response":"1_55d74c04eab36a0a018bb7a879a6f49f072b023690cba936"
  4. }

5.5 Disabling UserDetect in the App

You can call the shutdownUserDetect() API to disable UserDetect and release resources. The sample code is as follows:

Collapse
Word wrap
Dark theme
Copy code
  1. // Replace related parameters with your activity or context.
  2. SafetyDetectClient client = SafetyDetect.getClient(MainActivity.this);
  3. client.shutdownUserDetect().addOnSuccessListener(new OnSuccessListener<Void>() {
  4.     @Override
  5.     public void onSuccess(Void v) {
  6.         // Communication with the service was successful.
  7.     }
  8. }).addOnFailureListener(new OnFailureListener() {
  9.     @Override
  10.     public void onFailure(Exception e) {
  11.         // An error occurred during communication with the service.
  12.     }
  13. });

Common error codes

19800: DETECT FAIL

Detection failed. It is recommended that the UserDetect API be called again.

19002: NETWORK_ERROR

The network is not connected. It is recommended that the UserDetect API be called again when the network is connected.


Note:

For details about the request API, please refer to the API reference.

For details, click SafetyDetect-UserDetect-Sample.zip to download the sample code.



This page may contain third-party content. For details, click here.
Search in Guides
Enter a keyword.