通过以上步骤获取的access_token信息,发送HTTPS POST查询报表数据。
示例代码(Java)如下所示:
其中“reportUrl ”= “https://ads.cloud.huawei.com/openapi/monetization/reports/v1/publisher”。
俄罗斯站点“reportUrl ”= “https://ads-drru.cloud.huawei.ru/openapi/monetization/reports/v1/publisher”
/**
* 调用报表查询接口
* @param accessToken 根据clientId和密钥获取的token
* @throws IOException IO异常
*/
private static void callApiUseToken(String accessToken) throws IOException {
// 设置报文头Content-Type,Authorization
PostMethod postMethod = new PostMethod(reportUrl);
postMethod.setRequestHeader("Content-Type","application/json;charset=utf-8");
postMethod.setRequestHeader("Authorization","Bearer " + accessToken);
// 设置报文body
Map<String, Object> bodyMap = new HashMap<>();
Map<String, String> filterMap = new HashMap<>(16);
filterMap.put("currency", "CNY");
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);
}
}