摘要:本地生活商家或服务商在开发抖音小程序时,需要将小程序的商品入库,本地生活商家或服务商需要对接抖音提供的一系列接口,如创建商品,修改商品,商品上下架,同步库存等,笔者因为是用C#开发的,网上没有相关资料,特地摸索写了一套方式,与大家共勉。
本地生活商家或服务商在开发抖音小程序时,需要将小程序的商品入库,本地生活商家或服务商需要对接抖音提供的一系列接口,如创建商品,修改商品,商品上下架,同步库存等,笔者因为是用C#开发的,网上没有相关资料,特地摸索写了一套方式,与大家共勉。
一、Saas生活服务类小程序对接接口有以下几个:
二、根据接口文档,先创建/更新商品接口类:
///
/// 创建商品/更新商品接口,第一级实体类
///
[Serializable]
public class M_Product
{
public Api_ProductStruct product { get; set; }
public Api_SkuStruct sku { get; set; }
public string account_id { get; set; }
}
///
/// 第二级product实体
///
[Serializable]
public class Api_ProductStruct
{
public string product_id { get; set; }
public string out_id { get; set; }
public string product_name { get; set; }
public string category_full_name { get; set; }
public int category_id { get; set; }
public int product_type { get; set; }
public int product_sub_type { get; set; }
public int biz_line { get; set; }
public string account_name { get; set; }
public List telephone { get; set; }
public int sold_start_time { get; set; }
public int sold_end_time { get; set; }
public string out_url { get; set; }
public List pois { get; set; }
public Dictionary attr_key_value_map { get; set; }
public api_product_ext_struct product_ext { get; set; }
}
[Serializable]
public class api_pois_struct
{
public string poi_id { get; set; }
public string supplier_ext_id { get; set; }
}
[Serializable]
public class api_product_ext_struct
{
public bool auto_online { get; set; }
public api_test_extra_struct test_extra { get; set; }
}
[Serializable]
public class api_test_extra_struct
{
public List uids { get; set; }
public bool test_flag { get; set; }
}
///
/// 第二级sku实体
///
[Serializable]
public class Api_SkuStruct
{
public string sku_name { get; set; }
public int origin_amount { get; set; }
public int actual_amount { get; set; }
public api_stock_struct stock { get; set; }
public string out_sku_id { get; set; }
public int status { get; set; }
public Dictionary attr_key_value_map { get; set; }
}
[Serializable]
public class api_stock_struct
{
public int limit_type { get; set; }
public int stock_qty { get; set; }
public string out_sku_id { get; set; }
public int status { get; set; }
}
///
/// 返回实体
///
[Serializable]
public class api_response_struct
{
public api_data data { get; set; }
public api_extra extra { get; set; }
}
[Serializable]
public class api_data
{
public int error_code { get; set; }
public string description { get; set; }
public string product_id { get; set; }
}
[Serializable]
public class api_extra
{
public int error_code { get; set; }
public string description { get; set; }
public int sub_error_code { get; set; }
public string sub_description { get; set; }
public int now { get; set; }
public string logid { get; set; }
}
三、发送请求类:
public api_response_struct SendRequest1(string accessToken)
{
WebRequest webRequest = WebRequest.Create("https://open.douyin.com/goodlife/v1/goods/product/save/");
webRequest.Method = "POST";
webRequest.Headers.Add("access-token", accessToken);
string requestString = JsonConvert.SerializeObject(this);
byte requestBytes = Encoding.UTF8.GetBytes(requestString);
webRequest.GetRequestStream.Write(requestBytes, 0, requestBytes.Length);
WebResponse webResponse = webRequest.GetResponse;
StreamReader reader = new StreamReader(webResponse.GetResponseStream);
string responseString = reader.ReadToEnd;
return JsonConvert.DeserializeObject(responseString);
}
四、赋值调用请求方法得到结果 :
#region 抖音上架商品相关接口1
M_Product info4 = new M_Product;
info4.product = new Api_ProductStruct;
info4.sku = new Api_SkuStruct;
info4.product.category_id = 4025001; //演唱会
info4.product.category_full_name = "test_category";
// info4.Product.ProductId = "1";
info4.product.out_id = "55627";
info4.product.product_name = "票务商品测试";
info4.product.category_full_name = "票务商品测试";
info4.product.category_id = 1;
info4.product.product_type = 5; //5门票
// info4.Product.ProductSubType = 1;
info4.product.biz_line = 1;
info4.product.account_name = "中购文化";
info4.product.telephone = new List { "1234", "4321" };
info4.product.sold_start_time = 111111;
info4.product.sold_end_time = 2222222;
// info4.product.out_url = "http://xxx.cn";
info4.product.pois = new List
{
new api_pois_struct,
};
info4.product.pois[0].poi_id = "111111";
info4.product.pois[0].poi_id = "222222";
info4.product.pois[0].poi_id = "333333";
info4.product.pois[0].supplier_ext_id = "qqqqq";
info4.product.pois[1].supplier_ext_id = "wwwww";
info4.product.pois[2].supplier_ext_id = "eeeee";
info4.product.product_ext = new api_product_ext_struct;
info4.product.product_ext.auto_online = true;
info4.product.product_ext.test_extra = new api_test_extra_struct;
info4.product.product_ext.test_extra.uids = new List;
info4.product.product_ext.test_extra.test_flag = false;
info4.sku.sku_name = "testsku";
info4.sku.origin_amount = 5000;
info4.sku.actual_amount = 4000;
info4.sku.stock = new api_stock_struct;
info4.sku.stock.limit_type = 1;
info4.sku.stock.stock_qty = 100;
info4.sku.out_sku_id = "aaaaaa";
info4.sku.status = 1;
info4.product.attr_key_value_map = new Dictionary
{
{ "aaa", "111" },
{ "bbb", "222" }
};
info4.sku.attr_key_value_map = new Dictionary
{
{ "aaa", "11" },
{ "bbb", "22" }
};
api_response_struct resp1 = info3.SendRequest1(info1);
Console.WriteLine(JsonConvert.SerializeObject(resp));
#endregion 抖音上架商品相关接口1
综上所述,将测试参数换成自己的实际参数,抖音商品入库返回大功告成,可以将此方法改进成批量入库的,其它上下架,同步接口可以参照此方法,如果大家有更好的思路,欢迎留言!
来源:左手牵起右手