Protocol |
HTTPS POST |
|---|---|
Direction |
App server > Huawei UserDetect server |
URL |
https://hirms.cloud.huawei.com.cn/rms/v1/userRisks/verifyNoCaptcha?appId={appId} |
Data Format |
Request: Content-Type: application/json Response: Content-Type: application/json |
Parameter |
Mandatory |
Type |
Description |
|---|---|---|---|
appId |
Yes |
String |
App ID applied in AppGallery Connect. |
Request Header
None.
Request Body
Parameter |
Mandatory |
Type |
Description |
|---|---|---|---|
accessToken |
Yes |
String |
Access token. You can obtain an access token through the API provided by the open platform. For details, please refer to Client Credentials. |
response |
Yes |
String |
Response containing the fake user detection result returned to the app. |
POST https://{domain}/rms/v1/userRisks/verifyNoCaptcha?appId=******// For details about {domain}, please refer to Site Information.
{
"response": "1_06684cfc0ce72baf3b7badeb651b667eb7c0a40b4724192c_1601302283856",
"accessToken": "CgB6e3x9VDo9eewEj3/cCVt19xLg6Lnc4cOhqYRszjV7g2tnZwqHtvCd7fE7yFZe0RBN1zj4TacNOaJ+PuZuXjjg"
}
The following tables describe the response parameters when the status code is 200.
Response Header
Parameter |
Mandatory |
Type |
Description |
|---|---|---|---|
Content-Type |
Yes |
String |
Content type. The value is application/json. |
Response Body
Parameter |
Type |
Description |
|---|---|---|
success |
Boolean |
Fake user detection result. true: real user; false: fake user. |
challenge_ts |
String |
Time when the response token is generated. (ISO format: yyyy-MM-dd'T'HH:mm:ssZZ) |
apk_package_name |
String |
Package name of the app that calls the API. |
error-codes |
String |
Error codes returned upon API call exception. |
isHuaweiDevice |
Boolean |
Indicates whether the device is a Huawei device. |
isBotBehavior |
Boolean |
Indicates whether the behavior is from a bot. |
isEmulator |
Boolean |
Indicates whether the device is an emulator. |
{
"isEmulator": false,
"apk_package_name": "com.example.mirror",
"success": true,
"challenge_ts": "2020-09-28T22:11:23+0800",
"isBotBehavior": false,
"isHuaweiDevice": true
}
When you call the UserDetect server API, the value of rootUrl varies depending on the site. You need to select the nearest site, and generate the URL to access the API by concatenating the site's root URL and the API's URL.
Service |
Site Information {domain} |
UserDetect |
hirms.cloud.huawei.com.cn (Chinese mainland) |
Status Code |
Result Code |
Description |
Solution |
|---|---|---|---|
200 |
0 |
Success. |
- |
missing-input-at |
The accessToken parameter is missing. |
Add the accessToken parameter to the request body. |
|
invalid-input-at |
Invalid accessToken parameter. |
Verify that the accessToken parameter is valid. |
|
missing-input-response |
The response parameter is missing. |
Add the response parameter to the request body. |
|
invalid-input-response |
Invalid response parameter. |
Verify that the response parameter is valid. |
|
timeout-or-duplicate |
The response parameter has expired or has been used.
CAUTION:
The response token returned by the UserDetect API is valid only within 2 minutes and can be used only once. |
Call the UserDetect API again to obtain a new response token and use the token within the validity period. |
|
current-site-not-support |
This API cannot be called outside the Chinese mainland. |
- |
/**
* 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;
}