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
GuidesSafety DetectUserDetect API

UserDetect API

Service Features

  1. Fake users can be identified 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. Outside the Chinese mainland, users can be verified based on verification codes. If a user is suspicious or risky, a verification code is sent to the user for secondary verification.
  3. In the Chinese mainland, users cannot be verified based on verification codes. The nocaptcha API on the cloud is used to obtain the user detection result.

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

Service Process

  1. Your app integrates the Safety Detect SDK and calls the UserDetect API.
  2. Safety Detect evaluates risks of the device running your app. If the risk level is medium or high, it asks the user to enter a verification code and sends a response token to your app.
  3. Your app sends the response token to your app server.
  4. Your app server sends the response token to the Safety Detect server to obtain the check result.

Use Cases

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

App Development

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:
Java
Kotlin
Collapse
Word wrap
Dark theme
Copy code
  1. private void initUserDetect() {
  2. // Replace with your activity or context as a parameter.
  3. SafetyDetectClient client = SafetyDetect.getClient(MainActivity.this);
  4. client.initUserDetect().addOnSuccessListener(new OnSuccessListener<Void>() {
  5. @Override
  6. public void onSuccess(Void v) {
  7. // Indicates that communication with the service was successful.
  8. }
  9. }).addOnFailureListener(new OnFailureListener() {
  10. @Override
  11. public void onFailure(Exception e) {
  12. // An error occurred during communication with the service.
  13. }
  14. });
  15. }
Collapse
Word wrap
Dark theme
Copy code
  1. private fun initUserDetect() {
  2. // Replace with your activity or context as a parameter.
  3. val client = SafetyDetect.getClient(this@MainActivity)
  4. client.initUserDetect().addOnSuccessListener {
  5. // Indicates that communication with the service was successful.
  6. }.addOnFailureListener {
  7. // An error occurred during communication with the service.
  8. }
  9. }

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 object.

    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. Pass 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:
    Java
    Kotlin
    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 that 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. // An error occurred during communication 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. }
    Collapse
    Word wrap
    Dark theme
    Copy code
    1. override fun onClick(v: View?) {
    2. val client = SafetyDetect.getClient(activity)
    3. val appId = "your_app_id"
    4. client.userDetection(appId)
    5. .addOnSuccessListener { userDetectResponse ->
    6. // Indicates that communication with the service was successful.
    7. val responseToken = userDetectResponse.responseToken
    8. if (responseToken.isNotEmpty()) {
    9. // 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.
    10. }
    11. }
    12. .addOnFailureListener { // There was an error communicating with the service.
    13. val errorMsg: String? = if (it is ApiException) {
    14. // An error with the HMS API contains some additional details.
    15. // You can use the apiException.getStatusCode() method to get the status code.
    16. (SafetyDetectStatusCodes.getStatusCodeString(it.statusCode) + ": "
    17. + it.message)
    18. } else {
    19. // Unknown type of error has occurred.
    20. it.message
    21. }
    22. Log.i(TAG, "User detection fail. Error info: $errorMsg")
    23. }
    24. }
    NOTE

    If the UserDetect API has executed the onSuccess() method, the user has completed man-machine distinction. However, you still need to verify responseToken of the user on your app server.

  2. Call an on-cloud API (nocaptcha in the Chinese mainland and verify outside the Chinese mainland) based on the response token obtained in 1 to obtain the user detection result.

    To obtain the detection result, perform the following steps on the server:

    1. Obtain an access token. For details, please refer to OAuth 2.0-based Authentication.

    2. Call the cloud API to obtain the detection result. For details, please refer to Obtaining Fake User Detection Results (Outside the Chinese Mainland). The following is a request example:

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. POST https://{domain}/rms/v1/userRisks/verify?appId=****** HTTP/1.1// For details about {domain}, please refer to Site Information.
    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. }

Disabling UserDetect in the App

You can call the shutdownUserDetect API to disable UserDetect and release resources. The sample code is as follows:
Java
Kotlin
Collapse
Word wrap
Dark theme
Copy code
  1. private void shutdownUserDetect() {
  2. // Replace with your activity or context as a parameter.
  3. SafetyDetectClient client = SafetyDetect.getClient(MainActivity.this);
  4. client.shutdownUserDetect().addOnSuccessListener(new OnSuccessListener<Void>() {
  5. @Override
  6. public void onSuccess(Void v) {
  7. // Indicates that communication with the service was successful.
  8. }
  9. }).addOnFailureListener(new OnFailureListener() {
  10. @Override
  11. public void onFailure(Exception e) {
  12. // An error occurred during communication with the service.
  13. }
  14. });
  15. }
Collapse
Word wrap
Dark theme
Copy code
  1. private fun shutdownUserDetect() {
  2. // Replace with your activity or context as a parameter.
  3. val client = SafetyDetect.getClient(this@MainActivity)
  4. client.shutdownUserDetect()
  5. .addOnSuccessListener {
  6. // Indicates that communication with the service was successful.
  7. }.addOnFailureListener {
  8. // An error occurred during communication with the service.
  9. }
  10. }

Common Error Codes

DETECT_FAIL: 19800

Detection failed. Call the UserDetect API again.

NETWORK_ERROR: 19002

The network is not connected. Ensure that the network is connected and call the UserDetect API again.

NOTE

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

For details about the client verification logic, please refer to UserDetect in Client Sample Code.

For details about the server verification logic, please refer to UserDetect in Server Sample Code.

Search in Guides
Enter a keyword.