├── README_ZH.md ├── README.md ├── src └── com │ └── huawei │ └── ads │ └── demo │ └── RestfulInterfaceCallDemo.java └── LICENSE /README_ZH.md: -------------------------------------------------------------------------------- 1 | # 华为广告服务Java服务端示例代码 2 | 中文 | [English](https://github.com/HMS-Core/hms-ads-severdemo) 3 | ## 目录 4 | 5 | * [简介](#简介) 6 | * [安装](#安装) 7 | * [配置](#配置) 8 | * [环境要求](#环境要求) 9 | * [示例代码](#示例代码) 10 | * [授权许可](#授权许可) 11 | 12 | 13 | ## 简介 14 | 华为广告服务(HUAWEI Ads Kit)服务端示例代码向您介绍如何通过Java代码调用RESTful接口。 15 | 16 | ## 安装 17 | 确认Java环境准备就绪。 18 | 19 | ## 配置 20 | 为使用华为广告服务RESTful接口提供的功能,您需要在请求消息中设置相关参数。 21 | 22 | **access token**: 请求消息的令牌。获取过程请参见华为开发者联盟上的开发指南。 23 | 24 | **requestUrl**: URL,用来接入华为广告服务的流量变现服务报表API。详情请参见API参考。 25 | 26 | ## 环境要求 27 | JDK 1.7.0及以上版本。 28 | 29 | ## 示例代码 30 | 使用在华为开发者联盟上获得的client ID以及对应密钥,发送HTTPS POST请求,获取查询access token,发送HTTPS POST查询报表数据。 31 | 32 | 1). Send an message to create a dataCollector. 33 | You need to assign a value to the request body based on the corresponding format and use the POST method to send the message body to the corresponding URL. 34 | 35 | ## 技术支持 36 | 如果您对HMS Core还处于评估阶段,可在[Reddit社区](https://www.reddit.com/r/HuaweiDevelopers/)获取关于HMS Core的最新讯息,并与其他开发者交流见解。 37 | 38 | 如果您对使用HMS示例代码有疑问,请尝试: 39 | - 开发过程遇到问题上[Stack Overflow](https://stackoverflow.com/questions/tagged/huawei-mobile-services),在`huawei-mobile-services`标签下提问,有华为研发专家在线一对一解决您的问题。 40 | - 到[华为开发者论坛](https://forums.developer.huawei.com/forumPortal/en/home?fid=0101187876626530001) HMS Core板块与其他开发者进行交流。 41 | 42 | 如果您在尝试示例代码中遇到问题,请向仓库提交[issue](https://github.com/HMS-Core/hms-ads-severdemo/issues),也欢迎您提交[Pull Request](https://github.com/HMS-Core/hms-ads-severdemo/pulls)。 43 | 44 | ## 授权许可 45 | 华为广告服务Java服务端示例代码经过[Apache License, version 2.0](http://www.apache.org/licenses/LICENSE-2.0)授权许可。 46 | 47 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HMS Ads Kit Java Severdemo 2 | English | [中文](https://github.com/HMS-Core/hms-ads-severdemo/blob/master/README_ZH.md) 3 | ## Table of Contents 4 | 5 | * [Introduction](#introduction) 6 | * [Installation](#installation) 7 | * [Configuration ](#configuration ) 8 | * [Supported Environments](#supported-environments) 9 | * [Sample Code](#sample-code) 10 | * [License](#license) 11 | 12 | 13 | ## Introduction 14 | Java sample demonstrates how to use Java code to invoke the restful API of the Ads Kit. 15 | 16 | ## Installation 17 | Before using Java sample code, check whether the Java environment has been installed. 18 | 19 | ## Configuration 20 | To use functions provided by Ads Kit restful API, you need to set related parameters in the request. 21 | 22 | **access token**: the request token for the request, it can be obtained from the Developers Web. 23 | 24 | **requestUrl**: URL for accessing Ads Kit, please refer to the restful API References. 25 | 26 | ## Supported Environments 27 | The Java version is 1.7.0 or later. 28 | 29 | ## Sample Code 30 | The following describes methods to invoke a restful API. 31 | 32 | 1). Send an message to create a dataCollector. 33 | You need to assign a value to the request body based on the corresponding format and use the POST method to send the message body to the corresponding URL. 34 | 35 | ## Question or issues 36 | If you want to evaluate more about HMS Core, 37 | [HMSCore on Reddit](https://www.reddit.com/r/HuaweiDevelopers/) is for you to keep up with latest news about HMS Core, and to exchange insights with other developers. 38 | 39 | If you have questions about how to use HMS samples, try the following options: 40 | - [Stack Overflow](https://stackoverflow.com/questions/tagged/huawei-mobile-services) is the best place for any programming questions. Be sure to tag your question with 41 | `huawei-mobile-services`. 42 | - [Huawei Developer Forum](https://forums.developer.huawei.com/forumPortal/en/home?fid=0101187876626530001) HMS Core Module is great for general questions, or seeking recommendations and opinions. 43 | 44 | If you run into a bug in our samples, please submit an [issue](https://github.com/HMS-Core/hms-ads-severdemo/issues) to the Repository. Even better you can submit a [Pull Request](https://github.com/HMS-Core/hms-ads-severdemo/pulls) with a fix. 45 | 46 | ## License 47 | Ads Kit Java code sample is licensed under the [Apache License, version 2.0](http://www.apache.org/licenses/LICENSE-2.0). 48 | 49 | -------------------------------------------------------------------------------- /src/com/huawei/ads/demo/RestfulInterfaceCallDemo.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved. 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package com.huawei.ads.demo; 18 | 19 | import com.alibaba.fastjson.JSONObject; 20 | import org.apache.commons.httpclient.HttpClient; 21 | import org.apache.commons.httpclient.NameValuePair; 22 | import org.apache.commons.httpclient.methods.PostMethod; 23 | import org.apache.commons.httpclient.methods.RequestEntity; 24 | import org.apache.commons.httpclient.methods.StringRequestEntity; 25 | 26 | import java.io.IOException; 27 | import java.util.ArrayList; 28 | import java.util.HashMap; 29 | import java.util.List; 30 | import java.util.Map; 31 | 32 | 33 | public class RestfulInterfaceCallDemo { 34 | /** 35 | * Gateway address for OAuth authentication 36 | */ 37 | private static String tokenUrl = "https://login.cloud.huawei.com/oauth2/v3/token"; 38 | /** 39 | * OAuth2.0 client ID and key generated on HUAWEI Developers 40 | */ 41 | private static String clientId = "1024****"; // YOUR ID 42 | private static String secret = "a2c9***"; // YOUR KEY 43 | /** 44 | * Gateway for invoking data 45 | */ 46 | private static String reportUrl = "https://ads.cloud.huawei.com/openapi/monetization/reports/v1/publisher"; 47 | 48 | public static void main(String[] args) throws IOException { 49 | String token = getOauthToken(); 50 | callApiUseToken(token); 51 | } 52 | 53 | /** 54 | * Obtain token information 55 | * 56 | * @return Return the token value 57 | * @throws Throw the IOException exception 58 | */ 59 | private static String getOauthToken() throws IOException { 60 | // Set Content-Type in the packet header 61 | PostMethod postMethod = new PostMethod(tokenUrl); 62 | postMethod.setRequestHeader("Content-Type", 63 | "application/x-www-form-urlencoded;charset=utf-8"); 64 | 65 | // Set the packet body 66 | List param = new ArrayList<>(); 67 | param.add(new NameValuePair("grant_type", "client_credentials")); 68 | param.add(new NameValuePair("client_id", clientId)); 69 | param.add(new NameValuePair("client_secret", secret)); 70 | postMethod.setRequestBody(param.toArray(new NameValuePair[param.size()])); 71 | 72 | HttpClient httpClient = new HttpClient(); 73 | int ret = httpClient.executeMethod(postMethod); 74 | String accessToken = null; 75 | String tokenResponse = postMethod.getResponseBodyAsString(); 76 | if (ret == 200) { 77 | JSONObject atJson = JSONObject.parseObject(tokenResponse); 78 | accessToken = atJson.getString("access_token"); 79 | } else { 80 | System.out.println("request access token failed " + tokenResponse); 81 | } 82 | return accessToken; 83 | } 84 | 85 | /** 86 | * Call the Publisher Service Reporting API 87 | * @param accessToken Token obtained based on the client ID and key 88 | * @throws IOException 89 | */ 90 | private static void callApiUseToken(String accessToken) throws IOException { 91 | // Set the packet header.Content-Type, Authorization 92 | PostMethod postMethod = new PostMethod(reportUrl); 93 | postMethod.setRequestHeader("Content-Type","application/json;charset=utf-8"); 94 | postMethod.setRequestHeader("Authorization","Bearer " + accessToken); 95 | 96 | // Set the packet body 97 | Map bodyMap = new HashMap<>(); 98 | Map filterMap = new HashMap<>(16); 99 | filterMap.put("currency", "CNY"); 100 | bodyMap.put("filtering", filterMap); 101 | bodyMap.put("start_date", "2020-05-01"); 102 | bodyMap.put("end_date", "2020-06-20"); 103 | bodyMap.put("time_granularity", "STAT_TIME_GRANULARITY_DAILY"); 104 | bodyMap.put("order_type", "DESC"); 105 | 106 | RequestEntity requestEntity = new StringRequestEntity( JSONObject.toJSONString(bodyMap), 107 | "application/json" ,"UTF-8"); 108 | postMethod.setRequestEntity(requestEntity); 109 | 110 | HttpClient httpClient = new HttpClient(); 111 | int ret = httpClient.executeMethod(postMethod); 112 | String rpsContent = postMethod.getResponseBodyAsString(); 113 | if (ret == 200) { 114 | System.out.println(rpsContent); 115 | } else { 116 | System.out.println("callApi failed: ret =" + ret + " rsp=" + rpsContent); 117 | } 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | 3 | Version 2.0, January 2004 4 | 5 | http://www.apache.org/licenses/ 6 | 7 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 8 | 9 | 1. Definitions. 10 | 11 | "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. 16 | 17 | "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. 18 | 19 | "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. 20 | 21 | "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. 22 | 23 | "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). 24 | 25 | "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. 26 | 27 | "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." 28 | 29 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 30 | 31 | 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 32 | 33 | 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 34 | 35 | 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: 36 | 37 | You must give any other recipients of the Work or Derivative Works a copy of this License; and 38 | You must cause any modified files to carry prominent notices stating that You changed the files; and 39 | You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and 40 | If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. 41 | 42 | You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 43 | 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 44 | 45 | 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 46 | 47 | 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 48 | 49 | 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 50 | 51 | 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. 52 | 53 | END OF TERMS AND CONDITIONS --------------------------------------------------------------------------------