百度广告联盟api probuf协议对接

百度的广告API使用的是不是通常的http协议,而是使用谷歌开源出来的probuf协议,具体介绍请参考:https://www.jianshu.com/p/b1f18240f0c7https://www.jianshu.com/p/b1f18240f0c7

我们对接的客户端使用的是java,所以先会引用相对的jar包,使用的maven,如下:

<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.5.0</version>
</dependency> <dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>com.googlecode.protobuf-java-format</groupId>
<artifactId>protobuf-java-format</artifactId>
<version>1.2</version>
</dependency>

  

需要在windows下载probuf的exe文件,来根据协议生成java的实体class

https://download.csdn.net/download/can007/5816063 在这里下载

执行脚本来根据百度提供的 aidu_mobads_api_5.proto 文件生成java类

protoc.exe --java_out=./ baidu_mobads_api_5.proto

会生成一个这个:

百度广告联盟api probuf协议对接

然后使用的时候大概是这样:  (会注释掉敏感的)

BaiduMobadsApi5.App.Builder appBuilder = BaiduMobadsApi5.App.newBuilder();
BaiduMobadsApi5.Version.Builder appversionBuilder = BaiduMobadsApi5.Version.newBuilder();
appversionBuilder.setMajor(3);
appversionBuilder.setMinor(2);
appversionBuilder.setMicro(0);
appBuilder.setAppId("");
appBuilder.setAppPackage("");
appBuilder.setAppVersion(appversionBuilder); BaiduMobadsApi5.AdSlot.Builder adSlotBuilder = BaiduMobadsApi5.AdSlot.newBuilder();
BaiduMobadsApi5.Size.Builder sizeBuilder = BaiduMobadsApi5.Size.newBuilder();
sizeBuilder.setWidth(580);
sizeBuilder.setHeight(90);
adSlotBuilder.setAdslotId("");
adSlotBuilder.setAdslotSize(sizeBuilder); BaiduMobadsApi5.Device.Builder deviceBuilder = BaiduMobadsApi5.Device.newBuilder(); deviceBuilder.setDeviceType(BaiduMobadsApi5.Device.DeviceType.PHONE);
deviceBuilder.setOsType(OsType.ANDROID);
BaiduMobadsApi5.Version.Builder osversionBuilder = BaiduMobadsApi5.Version.newBuilder();
osversionBuilder.setMajor(8);
osversionBuilder.setMinor(1);
osversionBuilder.setMicro(0);
deviceBuilder.setOsVersion(osversionBuilder); deviceBuilder.setVendor(ByteString.copyFrom("OPPO".getBytes()));
deviceBuilder.setModel(ByteString.copyFrom("PADM00".getBytes()));
BaiduMobadsApi5.Size.Builder screensizeBuilder = BaiduMobadsApi5.Size.newBuilder();
screensizeBuilder.setWidth(800);
screensizeBuilder.setHeight(600); deviceBuilder.setScreenSize(screensizeBuilder); BaiduMobadsApi5.UdId.Builder udidBuilder = BaiduMobadsApi5.UdId.newBuilder();
udidBuilder.setImei("");
udidBuilder.setAndroidId("");
udidBuilder.setMac("");
deviceBuilder.setUdid(udidBuilder); BaiduMobadsApi5.Network.Builder networkBuilder = BaiduMobadsApi5.Network.newBuilder();
networkBuilder.setIpv4("");
networkBuilder.setConnectionType(ConnectionType.CELL_4G);
networkBuilder.setOperatorType(OperatorType.CHINA_MOBILE); String requestId =""; BaiduMobadsApi5.Version.Builder versionBuilder = BaiduMobadsApi5.Version.newBuilder();
versionBuilder.setMajor(5);
versionBuilder.setMinor(5);
versionBuilder.setMicro(0); MobadsRequest mobadsRequest = MobadsRequest.newBuilder().setRequestId(requestId).setApiVersion(versionBuilder)
.setApp(appBuilder).setAdslot(adSlotBuilder).setDevice(deviceBuilder).setNetwork(networkBuilder).build(); byte[] content = mobadsRequest.toByteArray(); BaiduMobadsApi5.MobadsRequest request = BaiduMobadsApi5.MobadsRequest.parseFrom(content);
System.out.println(request.getApp().getAppId()); System.out.println(mobadsRequest.getApp().getAppId()); HttpPost httpPost = new HttpPost("");
httpPost.setEntity(new ByteArrayEntity(content));
httpPost.setHeader("Content-type", "application/octet-stream;charset=utf-8");
CloseableHttpClient m_HttpClient = HttpClients.createDefault(); CloseableHttpResponse httpResponse = m_HttpClient.execute(httpPost); try {
HttpEntity entityResponse = httpResponse.getEntity();
BaiduMobadsApi5.MobadsResponse response = BaiduMobadsApi5.MobadsResponse.parseFrom(entityResponse.getContent());
// System.out.println(response.getErrorCode());
// System.out.println(response.getRequestId());
// System.out.println(response.getAdsList().get(0).getMaterialMeta().getImageSrc(0)); String jsonFormat =JsonFormat.printToString(response); System.out.println(jsonFormat); } finally {
httpResponse.close();
}

  

 

上一篇:在Ubuntu16.04.4和Windows10上安装jdk


下一篇:C#皮肤之IrisSkin4.dll