使用有赞商城API新增商品

本文主要参考 有赞API文档(Java版SDK):http://open.koudaitong.com/doc。


点我下载工程代码


启用有赞开放API

登陆店铺后台,点击应用和营销-有赞API,开启“有赞开放API”,就会看到AppID和AppSecret,这2个参数在后面程序GlobalConfi.java中要用到,请务必将工程里GlobalConfig.java中的APP_ID和APP_SECRETE改成你自己的(即下图中的两个值)

使用有赞商城API新增商品_第1张图片

图1 开启“有赞开放API”


使用有赞商城API新增商品_第2张图片

图2 选择商品品类

发布商品

总共有3步:选择商品品类,编辑基本信息,编辑商品详情。

选择商品品类

商品品类的编码如下,其中的cid是我们在程序中要用到的:

{ 
    "response": { 
        "categories": [ 
            { 
                "cid": 1000000, 
                "parent_cid": 0, 
                "name": "女人", 
                "is_parent": true, 
                "sub_categories": [ 
                    { 
                        "cid": 1000000, 
                        "parent_cid": 1000000, 
                        "name": "女人", 
                        "is_parent": false, 
                        "sub_categories": null
                    }
                ]
            }, 
            { 
                "cid": 2000000, 
                "parent_cid": 0, 
                "name": "男人", 
                "is_parent": true, 
                "sub_categories": [ 
                    { 
                        "cid": 2000000, 
                        "parent_cid": 2000000, 
                        "name": "男人", 
                        "is_parent": false, 
                        "sub_categories": null
                    }
                ]
            }, 
            { 
                "cid": 3000000, 
                "parent_cid": 0, 
                "name": "食品", 
                "is_parent": true, 
                "sub_categories": [ 
                    { 
                        "cid": 3000000, 
                        "parent_cid": 3000000, 
                        "name": "食品", 
                        "is_parent": false, 
                        "sub_categories": null
                    }
                ]
            }, 
            { 
                "cid": 4000000, 
                "parent_cid": 0, 
                "name": "美妆", 
                "is_parent": true, 
                "sub_categories": [ 
                    { 
                        "cid": 4000000, 
                        "parent_cid": 4000000, 
                        "name": "美妆", 
                        "is_parent": false, 
                        "sub_categories": null
                    }
                ]
            }, 
            { 
                "cid": 5000000, 
                "parent_cid": 0, 
                "name": "亲子", 
                "is_parent": true, 
                "sub_categories": [ 
                    { 
                        "cid": 5000000, 
                        "parent_cid": 5000000, 
                        "name": "亲子", 
                        "is_parent": false, 
                        "sub_categories": null
                    }
                ]
            }, 
            { 
                "cid": 6000000, 
                "parent_cid": 0, 
                "name": "居家", 
                "is_parent": true, 
                "sub_categories": [ 
                    { 
                        "cid": 6000000, 
                        "parent_cid": 6000000, 
                        "name": "居家", 
                        "is_parent": false, 
                        "sub_categories": null
                    }
                ]
            }, 
            { 
                "cid": 7000000, 
                "parent_cid": 0, 
                "name": "数码家电", 
                "is_parent": true, 
                "sub_categories": [ 
                    { 
                        "cid": 7000000, 
                        "parent_cid": 7000000, 
                        "name": "数码家电", 
                        "is_parent": false, 
                        "sub_categories": null
                    }
                ]
            }, 
            { 
                "cid": 8000000, 
                "parent_cid": 0, 
                "name": "其他", 
                "is_parent": true, 
                "sub_categories": [ 
                    { 
                        "cid": 8000001, 
                        "parent_cid": 8000000, 
                        "name": "礼品鲜花", 
                        "is_parent": false, 
                        "sub_categories": null
                    }, 
                    { 
                        "cid": 8000002, 
                        "parent_cid": 8000000, 
                        "name": "餐饮外卖", 
                        "is_parent": false, 
                        "sub_categories": null
                    }, 
                    { 
                        "cid": 8000003, 
                        "parent_cid": 8000000, 
                        "name": "丽人健身", 
                        "is_parent": false, 
                        "sub_categories": null
                    }, 
                    { 
                        "cid": 8000004, 
                        "parent_cid": 8000000, 
                        "name": "休闲娱乐", 
                        "is_parent": false, 
                        "sub_categories": null
                    }, 
                    { 
                        "cid": 8000005, 
                        "parent_cid": 8000000, 
                        "name": "酒店客栈", 
                        "is_parent": false, 
                        "sub_categories": null
                    }, 
                    { 
                        "cid": 8000006, 
                        "parent_cid": 8000000, 
                        "name": "婚庆摄影", 
                        "is_parent": false, 
                        "sub_categories": null
                    }, 
                    { 
                        "cid": 8000007, 
                        "parent_cid": 8000000, 
                        "name": "汽车养护", 
                        "is_parent": false, 
                        "sub_categories": null
                    }, 
                    { 
                        "cid": 8000008, 
                        "parent_cid": 8000000, 
                        "name": "家政服务", 
                        "is_parent": false, 
                        "sub_categories": null
                    }, 
                    { 
                        "cid": 8000009, 
                        "parent_cid": 8000000, 
                        "name": "门票卡券", 
                        "is_parent": false, 
                        "sub_categories": null
                    }, 
                    { 
                        "cid": 8000010, 
                        "parent_cid": 8000000, 
                        "name": "家装建材", 
                        "is_parent": false, 
                        "sub_categories": null
                    }, 
                    { 
                        "cid": 8000011, 
                        "parent_cid": 8000000, 
                        "name": "钟表眼镜", 
                        "is_parent": false, 
                        "sub_categories": null
                    }, 
                    { 
                        "cid": 8000012, 
                        "parent_cid": 8000000, 
                        "name": "宠物", 
                        "is_parent": false, 
                        "sub_categories": null
                    }, 
                    { 
                        "cid": 8000013, 
                        "parent_cid": 8000000, 
                        "name": "文化收藏", 
                        "is_parent": false, 
                        "sub_categories": null
                    }, 
                    { 
                        "cid": 8000014, 
                        "parent_cid": 8000000, 
                        "name": "日用百货", 
                        "is_parent": false, 
                        "sub_categories": null
                    }, 
                    { 
                        "cid": 8000015, 
                        "parent_cid": 8000000, 
                        "name": "教育培训", 
                        "is_parent": false, 
                        "sub_categories": null
                    }, 
                    { 
                        "cid": 8000016, 
                        "parent_cid": 8000000, 
                        "name": "媒体服务", 
                        "is_parent": false, 
                        "sub_categories": null
                    }, 
                    { 
                        "cid": 8000017, 
                        "parent_cid": 8000000, 
                        "name": "其他", 
                        "is_parent": false, 
                        "sub_categories": null
                    }
                ]
            }
        ]
    }
}


编辑基本信息

首先是商品分组,由于每个店铺有自己的商品分组,所以商品分组对应的编码,要使用程序去实时获取。Java程序如下:

	private static void getTags(){
		//String method = "kdt.item.get";
		String method = "kdt.itemcategories.tags.get";
		HashMap<String, String> params = new HashMap<String, String>();
		//params.put("num_iid", "2651514");
		//params.put("page_size", "20");
		
		KdtApiClient kdtApiClient;
		HttpResponse response;
		
		try {
			kdtApiClient = new KdtApiClient(APP_ID, APP_SECRET);
			response = kdtApiClient.get(method, params);
			System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
			StringBuffer result = new StringBuffer();
			String line = "";
			while ((line = bufferedReader.readLine()) != null) {
				result.append(line);
			}

			System.out.println(result.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

输出是xml格式的编码对照表:

{ 
    "response": { 
        "tags": [ 
            { 
                "id": 123456, 
                "item_num": 123, 
                "tag_url": "http://wap.koudaitong.com/v2/showcase/tag?alias=gsj1gw", 
                "type": "0", 
                "created": "2015-2-8 12:55:36", 
                "name": "分组1"
            }, 
            { 
                "id": 123456, 
                "item_num": 123, 
                "tag_url": "http://wap.koudaitong.com/v2/showcase/tag?alias=1dx1b9", 
                "type": "0", 
                "created": "2015-2-8 12:55:46", 
                "name": "分组2"
            }
        ]
    }
}

言归正传,说了这么多预备知识,终于等到主角出场了:新增商品。如图3,工程中需要用到httpcllient.jar等几个jar包。

使用有赞商城API新增商品_第3张图片

图3 Eclipse工程目录

直接上程序,不墨迹:

	private static void add() {
		
		// 新增商品
		String method = "kdt.item.add";
		
		// 商品参数
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("cid", "4000000"); // 商品品类
		params.put("price", "123"); // 价格
		params.put("title", "这是商品名称"); // 商品名称
		params.put("desc", "这是商品描述"); // 商品描述
		params.put("is_virtual", "0"); // 是否是虚拟商品
		params.put("post_fee", "0"); // 邮费
		params.put("tag_ids", "10500440"); // 商品分组
		params.put("quantity", "123"); // 库存
		params.put("sku_properties", "");
		params.put("sku_quantities", "");
		params.put("sku_prices", "");
		params.put("sku_outer_ids", "");
		
		// 商品图
		String fileKey = "images[]";
		List<String> filePaths = new ArrayList<String>();
		filePaths.add("C:\\Users\\cm\\Desktop\\refresh.png");
		filePaths.add("C:\\Users\\cm\\Desktop\\refresh.png");
		
		KdtApiClient kdtApiClient;
		HttpResponse response;
		
		try {
			kdtApiClient = new KdtApiClient(APP_ID, APP_SECRET);
			response = kdtApiClient.post(method, params, filePaths, fileKey);
			System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
			StringBuffer result = new StringBuffer();
			String line = "";
			while ((line = bufferedReader.readLine()) != null) {
				result.append(line);
			}

			System.out.println(result.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

点击“出售中的商品”,可以看到新增的商品!


点我下载工程代码


这是本人正在维护的一个关于眼睛护理的微信公众账号,专注IT白领人群,免费发布实用有趣的护眼知识,欢迎关注。

使用有赞商城API新增商品_第4张图片


你可能感兴趣的:(api,有赞商城)