关于java:抖音抖店开放平台SDK开源

144次阅读

共计 13731 个字符,预计需要花费 35 分钟才能阅读完成。

抖店 SDK

抖音 - 抖店开放平台 SDK:https://github.com/cnJun/sdk4…)API 接口调用和音讯推送验证解析,SDK 仅依赖 fastjson 来做 JSON 格局数据的解析。

API 接口

该 SDK 实用于自用型利用和工具型利用,他们的差异仅存在于获取受权的形式不一样:

获取调用 Client

  • 自用型利用

    对于自用型利用,不须要传 access token,SDK 中会主动进行 access token 的获取以及刷新解决(https://op.jinritemai.com/doc…)。

    DoudianClient client = new DefaultDoudianClient(serverUrl, appKey, appSecret);

  • 工具型利用

    工具型利用受权阐明:https://op.jinritemai.com/doc…

    对应工具型利用,判断店铺是否曾经受权,未受权须要先进行受权,受权胜利后须要将相干受权信息存储下来以便后续调用或者 access token 过期后进行刷新。

    • 店铺受权 (未受权或受权过期)

      用户受权胜利后,回调配置的地址,并返回 code 参数(url 参数)。应用 code 换取 access token 等受权信息。

      DoudianClient client = new DefaultDoudianClient(serverUrl, appKey, appSecret);
      if (!accessToken.isSuccess()) {

        // code 换取 access token 失败 

      } else {

        // 存储该受权店铺的信息
        // accessToken 受权 token
        // expiresIn 有效期 (秒)
        // shopId 店铺 id
        // shopName 店铺名
        // refreshToken 刷新 token
        // 计算 access token 过期工夫(向前推 1 分钟)int expiresIn = accessToken.getExpiresIn();
        long expire = System.currentTimeMillis() + (expiresIn - 60) * 1000;
        // 将上述相干信息进行存储 

      }

    • 依据受权信息获取 DoudianClient

      依据店铺 id 获取该店铺受权信息,判断 access token 是否已过期,过期则应用 refresh token 刷新受权,并将新的受权信息进行存储,以便后续应用。

      DoudianClient client = new DefaultDoudianClient(serverUrl, appKey, appSecret);

      // 依据店铺 id 获取店铺受权信息,并创立 accessToken 对象
      DoudianAccessToken accessToken = new DoudianAccessToken();
      accessToken.setAccessToken(店铺受权 token);
      accessToken.setExpiresIn(店铺受权 token 过期工夫);
      accessToken.setExpire(店铺受权 token 过期工夫点);
      accessToken.setRefreshToken(店铺受权刷新 token);

      client.setAccessToken(accessToken);
      if (!accessToken.isValid()) {

        accessToken = client.getAccessToken(accessToken.getRefreshToken());
        if (accessToken.isValid()) {
            // 存储该店铺新的受权信息
            // accessToken 受权 token
            // expiresIn 有效期 (秒)
            // shopId 店铺 id
            // shopName 店铺名
            // refreshToken 刷新 token
            // 计算 access token 过期工夫(向前推 1 分钟)int expiresIn = accessToken.getExpiresIn();
            long expire = System.currentTimeMillis() + (expiresIn - 60) * 1000;
        }

      }
      if (!accessToken.isValid()) {

        // 受权获取失败
        throw new RuntimeException("受权获取失败");

      }
      client.setAccessToken(accessToken);

SDK 调用类名规定

  • 申请类:将 method 参数,转换成相应的首字母大写驼峰名,而后加 Doudian 前缀,加 Request 后缀,即为 API 申请对象类名,例如:获取订单列表接口,method 为 order.list,转换为首字母大写驼峰 OrderList,再加前缀 / 后缀后,申请类名 DoudianOrderListRequest
  • 响应类:将 method 参数,转换成相应的首字母大写驼峰名,而后加 Doudian 前缀,加 Response 后缀,即为 API 申请对象类名,例如:获取订单列表接口,method 为 order.list,转换为首字母大写驼峰 OrderList,再加前缀 / 后缀后,响应类名 DoudianOrderListResponse
  • 调用形式:

    // 创立 client,自用型利用或工具型利用创立形式略有不同,详解上一节阐明
    DoudianClient client = getClient();
    // 构建申请类
    申请类 request = new 申请类 ();
    // 设置申请参数
    request.setXX1();
    request.setXX2();
    request.setXX3();
    ……
    // 调用 API
    响应类 response = client.execute();
    if (response.isSuccess()) {

      // 调用胜利,进行业务解决 

    } else {

      // 调用失败,进行错误处理
      // 错误代码:response.getErrNo();
      // 错误信息:response.getMessage();

    }

  • 对返回值的一些阐明

    • 目前已知接口,返回构造均为:

      {

      "err_no": 0, // 返回代码,0 示意胜利;非 0 示意失败
      "message": "success", // err_no 非 0 时示意调用错误信息
      "data": 返回的业务数据 (可能是各种构造)

      }

    • 对于分页类数据的返回,各个接口应用的参数名不一样,比方:获取商品列表接口将数据列表放在 data 上,而获取订单列表接口则将数据列表放在 list 上,为了对立解决分页类数据返回,能够通过如下办法来对立返回分页相干信息:

      DoudianPage< 业务数据类 > page = response.getData();
      page.getTotal(); // 总记录数
      page.getPageNumber(); // 当前页数
      page.getPageSize(); // 页大小
      List< 业务数据类 > list = page.getPageData(); // 业务数据列表

店铺 API

获取店铺的已受权品牌列表

API 接口:https://op.jinritemai.com/doc…

DoudianShopBrandListRequest request = new DoudianShopBrandListRequest();
DoudianShopBrandListResponse response = getClient().execute(request);
if (response.isSuccess()) {List<DoudianShopBrand> shopBrandList = response.getData();
    System.out.println("获取品牌信息:" + shopBrandList.size());
} else {System.out.println("获取失败:" + response.getMessage());
}
获取店铺后盾供商家公布商品的类目

API 接口:https://op.jinritemai.com/doc…

DoudianShopGetShopCategoryRequest request = new DoudianShopGetShopCategoryRequest();
request.setCid(0L);
DoudianShopGetShopCategoryResponse response = getClient().execute(request);
if (!response.isSuccess()) {System.out.println("获取失败:" + response.getMessage());
} else {for (DoudianShopCategory shopCategory : response.getData()) {System.out.print(repeat(" ", (shopCategory.getLevel() - 1) * 2));
        System.out.println(shopCategory.getId() + "," + shopCategory.getName());
    }
}

商品 API

获取商品详情

API 接口:https://op.jinritemai.com/doc…

DoudianProductDetailRequest request = new DoudianProductDetailRequest();
request.setProductId(3453347975240268172L);
DoudianProductDetailResponse response = getClient().execute(request);
if (!response.isSuccess()) {System.err.println("获取商品详情失败:" + response.getMessage());
} else {DoudianProduct product = response.getData();
    System.out.println(JSON.toJSONString(product));
}
获取商品列表

API 接口:https://op.jinritemai.com/doc…

DoudianProductListRequest request = new DoudianProductListRequest();
DoudianProductListResponse response = getClient().execute(request);
if (!response.isSuccess()) {System.err.println("获取商品列表失败:" + response.getMessage());
} else {for (DoudianProduct product : response.getData().getPageData()) {System.out.println(JSON.toJSONString(product));
    }
}
增加商品

API 接口:https://op.jinritemai.com/doc…

DoudianProductAddRequest request = new DoudianProductAddRequest();
request.setName("米奇牛仔裤");
request.setPic("https://sf6-ttcdn-tos.pstatp.com/obj/temai/2d5bb85c2e7f919f403ed6372a8ddc9fwww800-800");
request.setDescription("https://sf6-ttcdn-tos.pstatp.com/obj/temai/2d5bb85c2e7f919f403ed6372a8ddc9fwww800-800");
request.setMarketPrice(10000);
request.setDiscountPrice(10000);
request.setMobile("13400000001");
request.setWeight("110");
request.setProductFormat("{\" 货号 \":\"KZZL025\",\" 实用节令 \":\" 夏季 \"}");
request.setPayType(1);
request.setSpecId("134794584");
request.setCategoryLeafId("20597");
DoudianProductAddResponse response = getClient().execute(request);
if (!response.isSuccess()) {System.err.println("增加商品失败:" + response.getMessage());
} else {System.out.println("增加商品胜利:" + response.getData().getProductId());
}
编辑商品

API 接口:https://op.jinritemai.com/doc…

DoudianProductEditRequest request = new DoudianProductEditRequest();
request.setProductId(1L);
request.setName("米奇牛仔裤");
request.setPic("https://sf6-ttcdn-tos.pstatp.com/obj/temai/2d5bb85c2e7f919f403ed6372a8ddc9fwww800-800");
request.setDescription("https://sf6-ttcdn-tos.pstatp.com/obj/temai/2d5bb85c2e7f919f403ed6372a8ddc9fwww800-800");
request.setMarketPrice(10000);
request.setDiscountPrice(10000);
request.setMobile("13400000001");
request.setWeight("110");
request.setProductFormat("{\" 货号 \":\"KZZL025\",\" 实用节令 \":\" 夏季 \"}");
request.setPayType(1);
request.setSpecId("134794584");
request.setCategoryLeafId("20597");
DoudianProductEditResponse response = getClient().execute(request);
if (!response.isSuccess()) {System.err.println("编辑商品失败:" + response.getMessage());
} else {System.out.println("编辑商品胜利");
}
删除商品

API 接口:https://op.jinritemai.com/doc…

DoudianProductDelRequest request = new DoudianProductDelRequest();
request.setProductId(1L);
DoudianProductDelResponse response = getClient().execute(request);
if (!response.isSuccess()) {System.err.println("删除商品失败:" + response.getMessage());
} else {System.out.println("删除商品胜利");
}
增加规格

API 接口:https://op.jinritemai.com/doc…

DoudianSpecAddRequest request = new DoudianSpecAddRequest();
request.setSpecs("色彩 | 红色, 红色, 蓝色 ^ 尺码 |S,M,L");
request.setName("规格 2021-1");
DoudianSpecAddResponse response = getClient().execute(request);
if (!response.isSuccess()) {System.err.println("增加规格失败:" + response.getMessage());
} else {System.out.println("增加规格胜利");
}
获取规格详情

API 接口:https://op.jinritemai.com/doc…

DoudianSpecSpecDetailRequest request = new DoudianSpecSpecDetailRequest();
request.setId(145957862L);
DoudianSpecSpecDetailResponse response = getClient().execute(request);
if (!response.isSuccess()) {System.err.println("获取规格详情失败:" + response.getMessage());
} else {for (DoudianSpec spec : response.getData()) {System.out.println(JSON.toJSONString(spec));
    }
}
获取规格列表

API 接口:https://op.jinritemai.com/doc…

DoudianSpecListRequest request = new DoudianSpecListRequest();
DoudianSpecListResponse response = getClient().execute(request);
if (!response.isSuccess()) {System.err.println("获取规格列表失败:" + response.getMessage());
} else {for (DoudianSpec spec : response.getData()) {System.out.println(JSON.toJSONString(spec));
    }
}
删除规格

API 接口:https://op.jinritemai.com/doc…

DoudianSpecDelRequest request = new DoudianSpecDelRequest();
request.setId(145957862L);
DoudianSpecDelResponse response = getClient().execute(request);
if (!response.isSuccess()) {System.err.println("删除规格失败:" + response.getMessage());
} else {System.out.println("删除规格胜利");
}
增加 SKU

API 接口:https://op.jinritemai.com/doc…

DoudianSkuAddRequest request = new DoudianSkuAddRequest();
request.setProductId(3453183170894243915L);
request.setSpecId(115584464L);
request.setSpecDetailIds("981648008|981648010");
request.setStockNum(1);
request.setPrice(10100);
DoudianSkuAddResponse response = getClient().execute(request);
if (!response.isSuccess()) {System.err.println("增加 SKU 失败:" + response.getMessage());
} else {System.out.println("增加 SKU 胜利");
}
获取商品 sku 列表

API 接口:https://op.jinritemai.com/doc…

DoudianSkuListRequest request = new DoudianSkuListRequest();
request.setProductId(3453347975240268174L);
DoudianSkuListResponse response = getClient().execute(request);
if (!response.isSuccess()) {System.err.println("获取商品 sku 列表失败:" + response.getMessage());
} else {for (DoudianSku sku : response.getData()) {System.out.println(JSON.toJSONString(sku));
    }
}

订单 API

获取订单列表

API 接口:https://op.jinritemai.com/doc…

DoudianOrderListRequest request = new DoudianOrderListRequest();
request.setStartTime("2020-12-01 00:00:00");
request.setEndTime("2020-12-31 00:00:00");

DoudianOrderListResponse response = getClient().execute(request);
if (!response.isSuccess()) {System.err.println("获取订单列表失败:" + response.getMessage());
} else {DoudianPage<DoudianOrder> page = response.getData();
    for (DoudianOrder order : page.getPageData()) {System.out.println(JSON.toJSONString(order));
    }
}
获取订单详情

API 接口:https://op.jinritemai.com/doc…

DoudianOrderDetailRequest request = new DoudianOrderDetailRequest();
request.setOrderId("4736605172296554197A");
DoudianOrderDetailResponse response = getClient().execute(request);
if (!response.isSuccess()) {System.err.println("获取订单详情失败:" + response.getMessage());
} else {DoudianPage<DoudianOrder> page = response.getData();
    for (DoudianOrder order : page.getPageData()) {System.out.println(JSON.toJSONString(order));
    }
}
确认货到付款订单

API 接口:https://op.jinritemai.com/doc…

DoudianOrderStockUpRequest request = new DoudianOrderStockUpRequest();
request.setOrderId("4736605172296554197A");
DoudianOrderStockUpResponse response = getClient().execute(request);
if (!response.isSuccess()) {System.err.println("确认货到付款订单失败:" + response.getMessage());
} else {System.out.println("确认货到付款订单胜利");
}
勾销货到付款订单

API 接口:https://op.jinritemai.com/doc…

DoudianOrderCancelRequest request = new DoudianOrderCancelRequest();
request.setOrderId("4736605172296554197A");
DoudianOrderCancelResponse response = getClient().execute(request);
if (!response.isSuccess()) {System.err.println("勾销货到付款订单失败:" + response.getMessage());
} else {System.out.println("勾销货到付款订单胜利");
}
获取服务申请列表

API 接口:https://op.jinritemai.com/doc…

DoudianOrderServiceListRequest request = new DoudianOrderServiceListRequest();
DoudianOrderServiceListResponse response = getClient().execute(request);
if (!response.isSuccess()) {System.err.println("获取服务申请列表失败:" + response.getMessage());
} else {System.out.println("获取服务申请列表胜利");
}
回复服务申请

API 接口:https://op.jinritemai.com/doc…

DoudianOrderReplyServiceRequest request = new DoudianOrderReplyServiceRequest();
request.setId("1");
request.setReply("回复信息");
DoudianOrderReplyServiceResponse response = getClient().execute(request);
if (!response.isSuccess()) {System.err.println("回复失败:" + response.getMessage());
} else {System.out.println("回复胜利");
}
增加订单备注

API 接口:https://op.jinritemai.com/doc…

DoudianOrderAddOrderRemarkRequest request = new DoudianOrderAddOrderRemarkRequest();
request.setOrderId("4736605172296554197A");
request.setRemark("sh 订单");
request.setIsAddStar(true);
request.setStar(1);
DoudianOrderAddOrderRemarkResponse response = getClient().execute(request);
if (!response.isSuccess()) {System.err.println("增加订单备注失败:" + response.getMessage());
} else {System.out.println("增加订单备注胜利");
}

物流发货 API

获取快递公司列表

API 接口:https://op.jinritemai.com/doc…

DoudianOrderLogisticsCompanyListRequest request = new DoudianOrderLogisticsCompanyListRequest();
DoudianOrderLogisticsCompanyListResponse response = getClient().execute(request);
if (!response.isSuccess()) {System.err.println("获取快递公司列表失败:" + response.getMessage());
} else {for (DoudianOrderLogisticsCompany logisticsCompany : response.getData()) {System.out.println(JSON.toJSONString(logisticsCompany));
    }
}
订单发货

API 接口:https://op.jinritemai.com/doc…

DoudianOrderLogisticsAddRequest request = new DoudianOrderLogisticsAddRequest();
request.setOrderId("4736605172296524197A");
request.setLogisticsId(15L);
request.setCompany("中通快递");
request.setLogisticsCode("75417914223203");
DoudianOrderLogisticsAddResponse response = getClient().execute(request);
if (!response.isSuccess()) {System.err.println("发货失败:" + response.getMessage());
} else {System.out.println("发货胜利");
}
批改发货物流

API 接口:https://op.jinritemai.com/doc…

DoudianOrderLogisticsEditRequest request = new DoudianOrderLogisticsEditRequest();
request.setOrderId("4736605172296524197A");
request.setLogisticsId(15L);
request.setCompany("中通快递");
request.setLogisticsCode("75417914223203");
DoudianOrderLogisticsEditResponse response = getClient().execute(request);
if (!response.isSuccess()) {System.err.println("批改发货物流失败:" + response.getMessage());
} else {System.out.println("批改发货物流胜利");
}

售后退款 API

运费险 API

库存 API

账单 API

音讯推送

音讯推送服务接入指南:https://op.jinritemai.com/doc…

应用 SDK 验证解析抖店音讯推送示例:

@PostMapping(抖音店铺数据推送 URL,次要须要去掉权限验证)
public DouyinPushResult<Map> doudianShopDataPush(HttpServletRequest httpRequest,
                                                 HttpServletResponse httpResponse) {DoudianPushResult result = new DoudianPushResult();
    DoudianPushMessage pushMessage = DoudianUtils.getPushMessage(httpRequest);
    if (!pushMessage.isSuccess()) {result.setCode(40041);
        result.setMsg("解析推送数据失败");
    } else if (DoudianUtils.isEmpty(pushMessage.getData())) {result.setCode(40041);
        result.setMsg("解析推送数据为空");
    } else {List<DoudianPushData> pushDataList = pushMessage.getData();
        // 推送地址增加后,平台会立刻 Post 一条 "[{"tag":"0","msg_id":"0","data":"2020-09-10T16:27:56.52842897+08:00"}]" 的测试音讯,// 必须返回 {"code":0,"msg":"success"}
        if (pushDataList.size() == 1 && "0".equals(pushDataList.get(0).getTag())) {result.setCode(0);
            result.setMsg("success");
        } else {
            // 业务解决
            for (DoudianPushData pushData : pushDataList) {if ("100".equals(pushData.getTag())) {DoudianTradeTradeCreate100 tradeCreate100 = pushData.toObject(DoudianTradeTradeCreate100.class);
                    // 具体业务解决
                }
            }
            if (true) {
                // 接管解决胜利
                result.setCode(0);
                result.setMsg("success");
            } else {
                // 接管解决失败
                result.setCode(40044);
                result.setMsg("接管解决失败音讯");
            }
        }
    }
    return result;
}

电商 ERP 利用




正文完
 0