Send an HTTPS POST request using the client ID and key obtained from HUAWEI Developers to obtain an access token. For details, please refer to Client Credentials.
Send an HTTPS POST request with the obtained access token to query report data.
The sample Java code is as follows:
The value of reportUrl is https://ads.cloud.huawei.com/openapi/monetization/reports/v1/publisher.
The value of reportUrl for the Russia site is https://ads-drru.cloud.huawei.ru/openapi/monetization/reports/v1/publisher.
/**
* Call the Publisher Service Reporting API.
* @param accessToken Token obtained based on the client ID and key.
* @throws IOException I/O exception.
*/
private static void callApiUseToken(String accessToken) throws IOException {
// Set Content-Type and Authorization in the packet header.
PostMethod postMethod = new PostMethod(reportUrl);
postMethod.setRequestHeader("Content-Type","application/json;charset=utf-8");
postMethod.setRequestHeader("Authorization","Bearer " + accessToken);
// Set the packet body.
Map<String, Object> bodyMap = new HashMap<>();
Map<String, String> filterMap = new HashMap<>(16);
filterMap.put("currency", "EUR");
bodyMap.put("filtering", filterMap);
bodyMap.put("start_date", "2020-05-01");
bodyMap.put("end_date", "2020-06-20");
bodyMap.put("time_granularity", "STAT_TIME_GRANULARITY_DAILY");
bodyMap.put("order_type", "DESC");
RequestEntity requestEntity = new StringRequestEntity( JSONObject.toJSONString(bodyMap),
"application/json" ,"UTF-8");
postMethod.setRequestEntity(requestEntity);
HttpClient httpClient = new HttpClient();
int ret = httpClient.executeMethod(postMethod);
String rpsContent = postMethod.getResponseBodyAsString();
if (ret == 200) {
System.out.println(rpsContent);
} else {
System.out.println("callApi failed: ret =" + ret + " rsp=" + rpsContent);
}
}