獲取access_token鑒權(quán)信息
使用開發(fā)者聯(lián)盟界面獲得的client_id以及對(duì)應(yīng)密鑰,發(fā)送HTTPS POST請(qǐng)求,獲取查詢access_token。
根據(jù)access_token查詢變現(xiàn)報(bào)表信息
通過(guò)以上步驟獲取的access_token信息,發(fā)送HTTPS POST查詢報(bào)表數(shù)據(jù)。
示例代碼(Java)如下所示:
其中reportUrl="https://ads.cloud.huawei.com/openapi/monetization/reports/v1/publisher"。
/**
* 調(diào)用報(bào)表查詢接口
* @param accessToken 根據(jù)clientId 和密鑰獲取的token
* @throws IOException
*/
private static void callApiUseToken(String accessToken) throws IOException {
// 設(shè)置報(bào)文頭 Content-Type, Authorization
PostMethod postMethod = new PostMethod(reportUrl);
postMethod.setRequestHeader("Content-Type","application/json;charset=utf-8");
postMethod.setRequestHeader("Authorization","Bearer " + accessToken);
// 設(shè)置報(bào)文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ù)操作
檢查報(bào)表請(qǐng)求返回值是否正確(code=0),根據(jù)實(shí)際需要使用報(bào)表數(shù)據(jù)。