承载协议 |
HTTPS POST |
|---|---|
接口方向 |
应用服务器->华为虚假用户检测服务器 |
接口URL |
https://hirms.cloud.huawei.com.cn/rms/v1/userRisks/verifyNoCaptcha?appId={appId} |
数据格式 |
请求消息:Content-Type: application/json 响应消息:Content-Type: application/json |
Request Header
无
Request Body
参数 |
是否必选 |
参数类型 |
描述 |
|---|---|---|---|
accessToken |
是 |
String |
接入令牌AccessToken。可通过平台提供的接口获取,具体请参见客户端模式(Client Credentials)获取AccessToken。 |
response |
是 |
String |
App端虚假用户检测结果返回的响应。 |
POST https://{domain}/rms/v1/userRisks/verifyNoCaptcha?appId=******//{domain}:请参见站点信息。
{
"response": "1_06684cfc0ce72baf3b7badeb651b667eb7c0a40b4724192c_1601302283856",
"accessToken": "CgB6e3x9VDo9eewEj3/cCVt19xLg6Lnc4cOhqYRszjV7g2tnZwqHtvCd7fE7yFZe0RBN1zj4TacNOaJ+PuZuXjjg"
}
状态码为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 |
若接口调用异常,则会返回错误码。 |
isHuaweiDevice |
Boolean |
是否真实华为设备。 |
isBotBehavior |
Boolean |
疑似机器行为。 |
isEmulator |
Boolean |
疑似模拟器。 |
{
"isEmulator": false,
"apk_package_name": "com.example.mirror",
"success": true,
"challenge_ts": "2020-09-28T22:11:23+0800",
"isBotBehavior": false,
"isHuaweiDevice": true
}
调用虚假用户检测的服务端API时,在不同站点有不同的rootUrl。您应选择最近的站点访问。使用站点地址和后续章节中的接口URL拼接成完整的URL。
服务 |
站点信息{domain} |
虚假用户检测 |
hirms.cloud.huawei.com.cn(中国大陆地区) |
状态码 |
错误码 |
描述 |
处理建议 |
|---|---|---|---|
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 |
非中国大陆地区不支持该方式调用。 |
- |
/**
* apply access token
*
* @param baseUrl the address of OAUTH2
* @param grantType authorization type
* @param appId app id
* @param secretKey Secret Key
*/
private static String applyAccessToken(String baseUrl, String grantType, String appId, String secretKey)
throws CommonException {
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", grantType));
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 accessToken;
String response = getHttpResponseContent(httpPostRequest);
try {
JSONObject res = JSON.parseObject(response);
accessToken = res.get("access_token").toString();
} catch (Exception e) {
LOGGER.warn("fail to apply access token");
throw new CommonException("false", "fail to apply access token");
}
LOGGER.info("access token: {}", accessToken);
return accessToken;
}
------------------------------------------------------------------------------------------------------
/**
* get user detect result
*
* @param appId app id "******"
* @param supplier request parameter
*/
private String getverifyNoCaptchaResult(String verifyUrl, String appId, Supplier<JSONObject> supplier) throws
CommonException {
URIBuilder uriBuilder;
try {
uriBuilder = new URIBuilder(verifyUrl);
} catch (URISyntaxException e) {
LOGGER.error("fail to new URIBuilder, msg:{}, class:{}", e.getMessage(), e.getClass().getSimpleName());
throw new CommonException("false", "client-fail-new-URIBuilder");
}
uriBuilder.addParameter("appId", appId);
HttpPost httpPostRequest;
try {
httpPostRequest = new HttpPost(uriBuilder.build());
} catch (URISyntaxException e) {
LOGGER.warn("fail to new HttpPost, msg:{}, class:{}", e.getMessage(), e.getClass().getSimpleName());
throw new CommonException("false", "client-fail-new-HttpPost");
}
httpPostRequest.addHeader("content-type", "application/json");
httpPostRequest.addHeader("X-Client-Version", "1.0.1.102");
StringEntity entityData;
try {
entityData = new StringEntity(supplier.get().toString());
} catch (UnsupportedEncodingException e) {
LOGGER.warn("fail to new StringEntity, msg:{}, class:{}", e.getMessage(), e.getClass().getSimpleName());
throw new CommonException("false", "client-fail-new-StringEntity");
}
httpPostRequest.setEntity(entityData);
return getHttpResponseContent(httpPostRequest);
}
--------------------------------------------------------------------------------------------------
private static String getHttpResponseContent(HttpPost httpPostRequest) throws CommonException {
SSLContext sslcontext;
try {
sslcontext = SSLContexts.custom().loadTrustMaterial(null, (chain, authType) -> true).build();
} catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
LOGGER.error("fail to build sslcontext, msg:{}, class:{}", e.getMessage(), e.getClass().getSimpleName());
throw new CommonException("false", "fail to build sslcontext");
}
SSLConnectionSocketFactory sslsf =
new SSLConnectionSocketFactory(sslcontext, null, null, new NoopHostnameVerifier());
HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
HttpResponse httpResponse;
try {
httpResponse = httpClient.execute(httpPostRequest);
} catch (IOException e) {
LOGGER.error("fail to get HttpResponse, msg:{}, class:{}, url: {}", e.getMessage(),
e.getClass().getSimpleName(), httpPostRequest.getURI());
throw new CommonException("false", "client-fail-get-HttpResponse");
}
if (httpResponse.getStatusLine().getStatusCode() != SUCCESS_CODE) {
LOGGER.warn(httpResponse.toString());
throw new CommonException("false", "response is not 200");
}
String responseContent = "";
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
try {
responseContent = EntityUtils.toString(httpEntity, "UTF-8");
} catch (IOException e) {
LOGGER.warn("fail to gain responseContent, msg:{}, class:{}", e.getMessage(),
e.getClass().getSimpleName());
}
try {
EntityUtils.consume(httpEntity);
} catch (IOException e) {
LOGGER.warn("fail to consume HttpEntity, msg:{}, class:{}", e.getMessage(),
e.getClass().getSimpleName());
}
}
LOGGER.info("responseContent: {}, url: {}", responseContent, httpPostRequest.getURI());
return responseContent;
}