目录

1 系统特性

1.  基于实时风险分析引擎识别:API会在合法范围内采集相关数据并上传实时风险分析引擎来识别当前用户是否为虚假用户。

2.  提供验证码检测虚假用户:当前检测结果是可疑或风险用户,弹出验证码对用户进行二次验证。


2 典型场景

可以帮助开发者 APP防范批量注册、撞库攻击、活动“薅羊毛”、内容爬虫等行为。

3 产品概述

(1) app集成HMS SDK 调用HMS Safety Detect service。

(2) Safety Detect评估设备运行环境风险,如果风险等级为中或高,弹出验证码进行验证,并返回app一个responseToken。

(3) app提交responseToken至app服务端。

(4) app服务端发送responseToken至Safety Detect 服务端获取检测结果。

4 开发流程

请参考:开发流程

5 应用开发

5.1 调用 UserDetect API

调用UserDetect API分为两步:

1.  发起检测请求:调用UserDetect API的userDetection()方法以发起虚假用户检测请求,API会返回一个responseToken。

2.  获取检测结果:使用步骤1获取response token调用云端verify获取检测结果。

5.2 初始化虚假用户检测

UserDetect提供了行为检测能力,如果您希望您的虚假用户检测功能具备该能力,可以通过调用initUserDetect()接口初始化虚假用户检测,示例代码如下:

  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 communication with the service was successful.
  8.        }
  9.    }).addOnFailureListener(new OnFailureListener() {
  10.        @Override
  11.        public void onFailure(Exception e) {
  12.            // There was an error communicating with the service.
  13.        }
  14.    });
  15. }

5.3 发起检测请求

您需要通过调用userDetection发起检测。通常在用户点击UI组件(如一个按钮)后触发调用该方法。调用userDetection方法时,您需要做以下事情:

1.  传入您申请的appId作为该方法的参数。

2.  分别添加OnSuccessListener和OnFailureListener实例作为监听器。

3.  重写onSuccess、onFailure,处理对应的结果。

调用该方法的示例代码如下所示:

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

说明:

5.4 获取检测结果

服务端主要分为两步:

1.  获取accessToken;

2.  调用云侧API获取检测结果。


具体步骤如下:

获取accessToken:详细请参考开放平台鉴权相关内容描述。

调用云侧API获取检测结果:调用云侧获取结果。消息请求示例如下所示。详细请参考:云侧API

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

5.5 APP端关闭虚假用户检测

您可以通过调用shutdownUserDetect()接口关闭虚假用户检测功能,释放资源,示例代码如下:

  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 communication with the service was successful.
  8.        }
  9.    }).addOnFailureListener(new OnFailureListener() {
  10.        @Override
  11.        public void onFailure(Exception e) {
  12.            // There was an error communicating with the service.
  13.        }
  14.    });
  15. }

常见错误码处理

DETECT_FAIL:值19800

检测失败,建议重新调用User Detect API。

NETWORK_ERROR:值19002

未连接到互联网,建议在确保接入互联网后,再次调用User Detect API。

说明:

更多关于请求接口的详细信息可以参考API参考。

具体代码逻辑请见:SafetyDetect-UserDetect-Sample.zip。