淘宝开发者平台使用总结1

试水TB开发者平台,目前可调通添加商品,获取商品列表,以及添加“主动通知”监听。现把整个过程进行简要梳理。官方文档虽详细。

第一步:注册成为平台开发者,然后创建一个自己的应用。

这里请先注意一下在应用设置->应用证书中,包含MyAppTest的App key与App secret,还有SDK下载。

在沙箱里注册一个卖家帐号:名称替换是要以sandbox_开始

然后,就可以了。。。

关键是要理解TB的授权机制,可查看文档

先明白几个概念:

App key:App的唯一标识,有时也称为client_id(比如OAuth2.0根据授权号获取SesstionKey(有时也要UserToken)时)

App secret: App的密钥,有时也称为client_secret(同上)

SessionKey:用户授权后的标识,就向用户家的房门钥匙。

回调URL:当用户点击“授权”后,TB会调用这个URL,并把用户的sessionKey一并发过来。

授权有四种方式,原理是一样:

有一个app,有一个用户,用户想用这个app,就拿app key去TB的平台申请(通过一个包含了appkey的URL就可以),TB会跳出用户登陆界面,

提示用户先登陆,然后再二次验证(手机短信),确认后,点击“授权”,则TB把sessionKey回调给上述的URL。然后,app就有sessionKey了,

就可以进房间了。。。当然也可以不使用回调。

为了显示智商高,我们不使用回调。来吧,干货

1、

浏览器中输入:https://oauth.tbsandbox.com/authorize?response_type=code&client_id=1021688926&redirect_uri=urn:ietf:wg:oauth:2.0:oob&state=1212&view=web

注意:把上述的client_id替换为app的->应用/应用设置/沙箱环境管理

失败了吧!我就知道,你会把这个client_id写成app的正式id,再看上面注意的后半部分。

成功将会返回一个授权码

2、使用SDK获取token

 Map<String, String> param = new HashMap<String, String>();
  param.put("grant_type", "authorization_code");
  param.put("code","TKNTer7J2jN4w15Bl3Rze0XU25829"); 
  param.put("client_id", "102123688926");
  param.put("client_secret", "sandbox4054d5e4ssd6c4408be596b6669");
  param.put("redirect_uri", "urn:ietf:wg:oauth:2.0:oob");
  param.put("view", "web");
  // param.put("state", state);
  try {
   String responseJson = WebUtils.doPost(
     "https://oauth.tbsandbox.com/token", param, 3000, 3000);
   System.out.println(responseJson);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

返回的是seesionKey和refreshKey

上面已经提了在哪儿下载SDK了哦!

 再送一个刷新sessionkey的吧

3、刷新sessionKey

  

Map<String, String> param = new HashMap<String, String>();
  param.put("grant_type", "refresh_token");
  param.put("refresh_token",
    "6201f26c7a14733cab6a1b4c47bsdf61c6334ZZ6bcef9d9ceb3611768589");
  param.put("client_id", "1021688943426");
  param.put("client_secret", "sandbox405sdfs4d5e4d6c4408be596b6669");
  param.put("view", "web");
  // param.put("state", state);
  try {
   String responseJson = WebUtils.doPost(
     "https://oauth.tbsandbox.com/token", param, 3000, 3000);
   System.out.println(responseJson);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

  至此,我们持有的TA房间的钥匙了,嗯。。。是沙箱环境下,正式测试环境一个样(我还没有测,TB说的)。

  --------------------------------------文字不够代码凑--------------------------------------------------------------------------

  验证一下能不能往添加商品,注意:测试过程出现的CID是类目ID,店铺里可以定。

  public void testBaseAddItem() {
TaobaoClient client = new DefaultTaobaoClient(
"http://gw.api.tbsandbox.com/router/rest", "10241688926",
"sandbox4054d5e4d6c4408be596b6669", "xml", 600, 600,
Constants.SIGN_METHOD_HMAC);
ItemAddRequest req = new ItemAddRequest();
req.setNum(1L);
req.setPrice("100");
req.setType("fixed");
req.setStuffStatus("new");
req.setTitle("沙箱测试ONE");
req.setDesc("描述最少5字符,最多不能超过25000字符");
req.setLocationState("浙江");
req.setLocationCity("杭州");
req.setCid(162104L);
req.setPaimaiInfoDeposit(10000L);
try {
ItemAddResponse response = client
.execute(req,
"61025014e19374100adf18fcceb81ab4ba26aced3d556842a3611768589");
System.out.println(response.getMsg());
} catch (ApiException e) {
e.printStackTrace();
}
}

--------------------------------------主动通知--------------------------------------------------------------------------------

原理:提交一个主动通知请求,然后建立一个长连接,TB就会根据请求的条件,将增量数据实时回调给长连接下的Listener,嗯,代码不忽悠。

TaobaoClient client = new DefaultTaobaoClient(
"http://gw.api.tbsandbox.com/router/rest", "1021688926",
"sandbox4054d5e4d6c4408be596b6669");
IncrementCustomerPermitRequest req = new IncrementCustomerPermitRequest();
req.setType("get,syn,notify");
req.setTopics("trade;refund;item");
req.setStatus("all;all;ItemAdd,ItemUpdate");
try {
IncrementCustomerPermitResponse response = client
.execute(req,
"62025304ea18fcceb81ab4ba26aced3d556842ZZb4e7fca3611768589");
System.out.println(response.getBody());
} catch (ApiException e) {
e.printStackTrace();
}

Configuration conf = new Configuration("1021688926",
"sandbox4054d5e4d6c4408be596b6669", null);
conf.setConnectUrl("http://stream.api.tbsandbox.com/stream");
TopCometStream stream = new TopCometStreamFactory(conf).getInstance();
stream.setConnectionListener(new InnerConnectionLifeCycleListener());
stream.setMessageListener(new InnerTopCometMessageListener());
stream.start();

测试的话,可以再注册一个买家号,购买沙箱中那个卖家下的商品,回调就来了。

代码中的app key或者app secret请换掉!

猜你喜欢

转载自huihuilou.iteye.com/blog/1986297