獲取access_token鑒權信息
使用開發(fā)者聯(lián)盟界面獲得的client_id以及對應密鑰,發(fā)送HTTPS POST請求,獲取查詢access_token。
根據(jù)access_token查詢變現(xiàn)報表信息
通過以上步驟獲取的access_token信息,發(fā)送HTTPS POST查詢報表數(shù)據(jù)。
示例代碼(Java)如下所示:
其中reportUrl="https://ads.cloud.huawei.com/openapi/monetization/reports/v1/publisher"。
/**
* 調用報表查詢接口
* @param accessToken 根據(jù)clientId 和密鑰獲取的token
* @throws IOException
*/
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);
}
}
后續(xù)操作
檢查報表請求返回值是否正確(code=0),根據(jù)實際需要使用報表數(shù)據(jù)。