C#通过API实现抓取wish店铺订单

  1. 开发者申请
  2. 认证授权
  3. 订单抓取
  4. 订单状态更新

授权流程

  • 官网申请开发者
  • 创建应用APP(勾选响应的权限)
  • 发布应用(得到clientid,client_secret )
  • 通过授权码获取店铺授权token以及refreshtoken,并保存到数据

token刷新代码

        /// <summary>
        /// 获取获取有效token
        /// </summary>
        /// <param name="storeInfoDto"></param>
        /// <returns></returns>

        private async Task<string> GetAccessToken(WishStoreInfoDto storeInfoDto)
        {
            if (storeInfoDto.Oauthorization_date <= DateTime.UtcNow.AddMinutes(-1))
            {
                var tokenString = await _httpClient.GetAsync("https://merchant.wish.com/api/v3/oauth/refresh_token", 
                    new Dictionary<string, string>() { { "Authorization", storeInfoDto.refresh_token } }
                    );
                var newToken = JObject.Parse(tokenString);
                storeInfoDto.Token = newToken.Value<string>("access_token");
                storeInfoDto.Oauthorization_date = newToken.Value<DateTime>("expiry_time");
                storeInfoDto.refresh_token = newToken.Value<string>("refresh_token");  //利用废弃字段存储token
                await _storeInfoAppService.UpdateStoreAuthInfoAsync(storeInfoDto);

                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine($"刷新token有效期至{storeInfoDto.Oauthorization_date}");
                Console.ForegroundColor = ConsoleColor.White;
            }
            return storeInfoDto.Token;

        }

订单抓取(直接上代码)

// 主要API merchant.wish.com/api/v3/orders
await _httpClient.GetAsync($"https://merchant.wish.com/api/v3/orders?limit=500&states={"APPROVED"}&released_at_min={lastDateTime}&sort_by=released_at&order_type=WISH_EXPRESS", new Dictionary<string, string>() { { "Authorization", await GetAccessToken(storeInfo) } });

接口内容解析成jarray数组,按官方文档解析即可,导入到erp或公司内部系统

发货通知及物流单号推送

return new WishSalesOrderDto()
            {
                Currency = (order["order_payment"] != null) ? order["general_payment_details"]["payment_total"].Value<string>("currency_code") : "USD",
                Customer_info = order["order_payment"].ToJsonString(),
                Customer_receiver = order["full_address"].IsNullOrEmpty() ? "{}" : order["full_address"].ToJsonString(),
                Is_wholesale = true,
                Items = order["product_information"].ToJsonString(),
                San_code = order.Value<string>("id"),
                Sb_id = order.Value<string>("id"),
                Create_at = order.Value<DateTime>("released_at"),
                Create_date = order.Value<DateTime>("released_at"),
                Package_info = order.Value<string>("tracking_information"),
                //发货号 需要保存 上传物流跟踪单号需要用到
                //Supplier_account_fullname = order.Value<string>("posting_number"),
                Supplier_sender_branch_fullname = "tvcmall-wish"
            };

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值