目录

获取虚假用户检测结果(非中国大陆地区)

功能介绍

您通过华为虚假用户检测服务,获取虚假用户检测的最终结果。

场景描述

您通过华为虚假用户检测服务获取最终检测结果。

使用约束

检测虚假用户,在非中国大陆地区使用。

接口原型

承载协议

HTTPS POST

接口方向

应用服务器->华为虚假用户检测服务器

接口URL

https://{domain}/rms/v1/userRisks/verify?appId={appId}

{domain}:请参见站点信息。

数据格式

请求消息:Content-Type: application/json

响应消息:Content-Type: application/json

查询参数

参数

是否必选

参数类型

描述

appId

是

String

您在AppGallery Connect平台上申请的应用appId。

请求参数

Request Header

无

Request Body

参数

是否必选

参数类型

描述

accessToken

是

String

接入令牌AccessToken。可通过平台提供的接口获取,具体请参见客户端模式(Client Credentials)获取AccessToken。

response

是

String

App端虚假用户检测结果返回的响应。

请求示例

POST https://{domain}/rms/v1/userRisks/verify?appId=******//{domain}:请参见站点信息。
{
"accessToken":"CV7Qxu7U0aqtFYxj9FIw2LcOaFpjsHBSHUz8lrGuTipIB2VJNUkBK630+WMCLxzti5xL
PxjYB6slP49sbc3vPY53XjM5",
"response":"1_76deea6daf1ce20995e2b55e4651a8d6f2ffa8a7a6dfe5ce_15907"
}

响应参数

状态码为200时:

Response Header

参数

是否必选

参数类型

描述

Content-Type

是

String

取值为:application/json。

Response Body

参数

参数类型

描述

success

Boolean

检测结果,true代表真实用户,false代表虚假用户。

challenge_ts

String

responseToken生成时间(iSO格式为yyyy-MM-dd’T’HH:mm:ssZZ)。

apk_package_name

String

调用检测接口的客户端的包名。

error-codes

String

若接口调用异常,则会返回错误码。

响应示例

HTTP/1.1 200 OK
Content-Type: application/json
{
"success":true,
"challenge_ts":"2020-05-29T15:32:53+0800",
"apk_package_name":"com.example.mockthirdapp"
}

错误码

状态码

错误码

描述

处理建议

200

0

成功。

-

missing-input-at

accessToken参数缺失。

在请求体中添加accessToken参数。

invalid-input-at

accessToken参数不合法。

检查accessToken参数是否合法。

missing-input-response

response参数缺失。

在请求体里面添加response参数。

invalid-input-response

response参数不合法。

检查response参数是否合法。

timeout-or-duplicate

response参数失效,已经过期或者已经被使用过。

注意:

人机检测接口返回的responseToken有效期为2分钟,且只能被使用1次。

调用App端虚假用户检测接口以重新获取ResponseToken,并在有效期内使用该token。

current-site-not-support

中国大陆地区不支持该方式调用。

-

站点信息

调用虚假用户检测的服务端API时,在不同站点有不同的rootUrl。您应选择最近的站点访问。使用站点地址和后续章节中的接口URL拼接成完整的URL。

服务

站点信息{domain}

虚假用户检测

hirms.cloud.huawei.asia (亚洲、非洲)

hirms.cloud.huawei.lat (拉美)

hirms.cloud.huawei.ru (俄罗斯)

hirms.cloud.huawei.eu (欧洲)

hirms.cloud.huawei.com.au (大洋洲)

说明:

非洲站点正在规划中,优先接入亚洲站点。

调用示例

/**
     * apply access token
     *
     * @param baseUrl   the address of OAUTH2
     * @param appId     app id
     * @param secretKey Secret Key
     */
    private static String applyAccessToken(String baseUrl, String appId, String secretKey) {
        HttpPost httpPostRequest = new HttpPost(baseUrl);
        httpPostRequest.setHeader("content-type", "application/x-www-form-urlencoded");
 
        List<NameValuePair> entityData = new ArrayList<>();
        entityData.add(new BasicNameValuePair("grant_type", "client_credentials"));
        entityData.add(new BasicNameValuePair("client_id", appId));
        entityData.add(new BasicNameValuePair("client_secret", secretKey));
        UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(entityData, StandardCharsets.UTF_8);
        httpPostRequest.setEntity(urlEncodedFormEntity);
 
        String response = execute(httpPostRequest);
        return JSON.parseObject(response).get("access_token").toString();
}
----------------------------------------------------
/**
     * get user detect result
     *
     * @param verifyUrl     https://{domain}/rms/v1/userRisks/verify?appId=******//{domain}:请参见站点信息。
     * @param appId         app id ""******""
     * @param accessToken   the access token which apply from OAUTH2 applyAccessToken get
     * @param responseToken the response token that returned by userDetection function
     */
    private String verifyUserRisks(String verifyUrl, String appId, String accessToken, String responseToken) {
        URIBuilder uriBuilder;
        URI uri;
        try {
            uriBuilder = new URIBuilder(verifyUrl);
            uriBuilder.addParameter("appId", appId);
            uri = uriBuilder.build();
        } catch (URISyntaxException e) {
            LOGGER.error("fail to create URI, msg:{}, class:{}", e.getMessage(), e.getClass().getSimpleName());
            return "";
        }
 
        HttpPost httpPostRequest;
        httpPostRequest = new HttpPost(uri);
        httpPostRequest.addHeader("content-type", "application/json");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("accessToken", accessToken);
        jsonObject.put("response", JSONObject.parseObject(responseToken).get("response"));
        StringEntity entityData;
        try {
            entityData = new StringEntity(jsonObject.toString());
        } catch (UnsupportedEncodingException e) {
            LOGGER.error("fail to new StringEntity, msg:{}, class:{}", e.getMessage(), e.getClass().getSimpleName());
            return "";
        }
        httpPostRequest.setEntity(entityData);
 
        return execute(httpPostRequest);
    }
-------------------------------------------------------------------------
private static String execute(HttpPost httpPostRequest) {
        SSLContext sslcontext = null;
        try {
            sslcontext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
                @Override
                public boolean isTrusted(X509Certificate[] chain, String authType) {
                    return true;
                }
            }).build();
        } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
            LOGGER.error("fail to build sslcontext, msg:{}, class:{}", e.getMessage(), e.getClass().getSimpleName());
        }
        SSLConnectionSocketFactory sslConnectionSocketFactory =
                new SSLConnectionSocketFactory(sslcontext, null, null, new NoopHostnameVerifier());
 
        HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslConnectionSocketFactory).build();
        HttpResponse httpResponse;
        try {
            httpResponse = httpClient.execute(httpPostRequest);
        } catch (IOException e) {
            LOGGER.error("fail to create HttpResponse, msg:{}, class:{}", e.getMessage(), e.getClass().getSimpleName());
            return "";
        }
 
        String responseContent = "";
        if (httpResponse.getStatusLine().getStatusCode() == SUCCESS_CODE) {
            HttpEntity httpEntity = httpResponse.getEntity();
            if (httpEntity != null) {
                try {
                    responseContent = EntityUtils.toString(httpEntity, "UTF-8");
                } catch (IOException e) {
                    LOGGER.error("fail to get the entity content as a String, msg:{}, class:{}", e.getMessage(),
                            e.getClass().getSimpleName());
                }
                try {
                    EntityUtils.consume(httpEntity);
                } catch (IOException e) {
                    LOGGER.error("fail to consume HttpEntity, msg:{}, class:{}", e.getMessage(),
                            e.getClass().getSimpleName());
                }
            }
        }
        return responseContent;
    }