最近对接了天猫接口,先说一下如何对接天猫订阅消息。
对接天猫订阅消息步骤:
1.在天猫开通对接接口权限,怎么开通直接联系天猫小二就行。不同的类目开通流程有所不同。
2.获取天猫的appkey, appSecret
3.天猫中开通订阅消息
4.调用端,开通订阅消息授权
public String tmcUserPermit() {
TaobaoClient client = new DefaultTaobaoClient(url, appkey, appSecret);
TmcUserPermitRequest req = new TmcUserPermitRequest();
req.setTopics("tmall_fuwu_AnomalyRecourse,tmall_fuwu_AnomalyRecourseStatusUpdate,tmall_fuwu_SettleAdjustmentStatusUpdate");
TmcUserPermitResponse response;
try {
response = client.execute(req, sessionkey);
return response.getBody();
} catch (ApiException e) {
e.printStackTrace();
}
return "没有获取到数据";
}
注意:
1)以前授权的订阅消息,不能删掉,删掉后表示取消以前的订阅消息;
2)多个订阅消息间,用逗号分开
5.编写代码接受和解析订阅消息
Java实现:
public void tmall_fuwu_WorkcardInfo_SDK() throws Exception {
TmcClient client = new TmcClient(wsurl, appkey, appSecret, "default");//
client.setMessageHandler(new MessageHandler() {
public void onMessage(Message message, MessageStatus status) {
try {
String topic=message.getTopic();
String context = message.getContent();
if ("tmall_fuwu_WorkcardInfo".equalsIgnoreCase(topic)) {
context = context.replace("\\\"", "'");
JsonConverter json = new JsonConverter();
ServiceTaskReponse serviceTask = json.toResponse(
context, ServiceTaskReponse.class);
System.out.println(serviceTask);
}
} catch (Exception e) {
e.printStackTrace();
status.fail();//
}
}
});
client.connect();
Thread.sleep(10000);
}
其中:
status.fail();这行代码很关键,表示告知天猫接受失败,天猫会自动重新传输。