目录

调用流量变现报表API

获取access_token鉴权信息

使用开发者联盟界面获得的客户端ID以及对应密钥,发送HTTPS POST请求,获取查询access_token。获取方式请参见客户端模式。

根据access_token查询变现报表信息

通过以上步骤获取的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);
        }
    }

后续操作

检查报表请求返回值是否正确(code = 0,即返回正确),根据实际需要使用报表数据。