[Huawei Developers] John Wo:
Part 1 - Obtaining an Access Token

a) Obtain an access token.

You can use the app ID and secret key of your app to request an access token from the Huawei authentication server. The sample code is as follows:

<p style="line-height: 1.5em;">private static String applyAccessToken(String appid, String secretKey) {
// Construct an HTTP POST request object.
HttpPost httpPostRequest = new HttpPost("https://oauth-login.cloud.huawei.com/oauth2/v2/token");
// Set the content type.
httpPostRequest.setHeader("content-type", "application/x-www-form-urlencoded");
// Construct the message body.
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);
// Execute the HTTP POST request.
String response = execute(httpPostRequest);
// Return the access token extracted from the response body.
return JSON.parseObject(response).get("access_token").toString();
}
</p>

An example of the request is as follows:

<p style="line-height: 1.5em;">POST /oauth2/v2/token HTTP/1.1
Host: oauth-login.cloud.huawei.com
Content-Type: application/x-www-form-urlencoded
grant_type=client_credentials&client_id=12345&client_secret=bKaZ0VE3EYrXaXCdCe3d2k9few
</p>

Part 2 - Obtaining the Result

b) Call the check result query API of the UserDetect server to obtain the check result.

Send a query request containing the app ID, access token, and response token to the UserDetect server. The sample code is as follows:

<p style="line-height: 1.5em;">public static String sendRmsMessage(String appid, String appSecret, String responseToken) {
// 1. Construct a HTTP POST request object.
HttpPost httpPostRequest;
// 1.1 Concatenate the URI of the UserDetect server.
URI uri = buildUri(appid);
httpPostRequest = new HttpPost(uri);
httpPostRequest.addHeader("content-type", "application/json");
StringEntity entityData;
try {
// 1.2 Construct the message body.
JSONObject messageObject = new JSONObject();
// 1.2.1 Add the access token to the message body.
messageObject.put("accessToken", applyAccessToken(appid, appSecret));
// 1.2.2 Add the response token to the message body.
messageObject.put("response", responseToken);
entityData = new StringEntity(messageObject.toString());
} catch (UnsupportedEncodingException e) {
Log.catching(e);
return "";
}
httpPostRequest.setEntity(entityData);
// 2. Execute the HTTP POST request.
return execute(httpPostRequest);
}
// URL builder.
private static URI buildUri(String appid) {
URIBuilder uriBuilder;
URI uri = null;
try {
// URL of the UserDetect server API.
uriBuilder = new URIBuilder("https://rms-drcn.platform.dbankcloud.com/rms/v1/userRisks/verify");
// Add the app ID to the URL.
uriBuilder.addParameter("appid", appid);
uri = uriBuilder.build();
} catch (URISyntaxException e) {
Log.catching(e);
}
return uri;
}
</p>

The following is an example of the check result query request:

<p style="line-height: 1.5em;">POST https://hirms.cloud.huawei.com/rms/v1/userRisks/verify?appId=101294943
{
"accessToken":"CV7Qxu7U0aqtFYxj9FIw2LcOaFpjsHBSHUz8lrGuTipIB2VJNUkBK630+WMCLxzti5xL
PxjYB6slP49sbc3vPY53XjM5",
"response":"1_76deea6daf1ce20995e2b55e4651a8d6f2ffa8a7a6dfe5ce_15907"
}
</p>

The following is an example of the response sent by the UserDetect server:

<p style="line-height: 1.5em;">Content-Type: application/json
{
"success": true,
"challenge_ts":" 2020-05-29T15:32:53+0800"
"apk_package_name":"com.example.mockthirdapp"
}
</p>

More details, you can check https://forums.developer.huawei.com/forumPortal/en/topic/0202460790665160012