自学内容网 自学内容网

【团购核销】抖音生活服务商家应用快速接入③——团购核销验券

一、前言

目的:将抖音团购核销的功能集成到我们自己开发的App和小程序中

二、获取token

public static String getAccessToken(){
    try {
        String objectStr = HttpUtil.get(String.format("https://open.douyin.com/oauth/client_token/?grant_type=client_credential&client_key=%s&client_secret=%s", APPID, AppSecret));
        log.info(objectStr);
        JSONObject jsonObject = JSON.parseObject(objectStr);
        return jsonObject.getJSONObject("data").getString("access_token");
    }catch (Exception e){
        e.printStackTrace();
        log.error("getAccessToken", e.getMessage());
    }
    return null;
}

三、验券准备

  • 验券准备是多出一步确认步骤,让用户选择确认。

在这里插入图片描述

  • 确认这是抖音团购券,而不是其他美团、快手的团购券。
  • 确认这团购券是该商家的,而不是其他同名店铺的。
  • 用户确认核销该团购券,毕竟核销后,就不能自动退款了。
  • 开发者文档验券准备_生活服务商家应用_抖音开放平台
public static String certificatePrepare(String shortUrl){
    try {
        log.info("短链:"+shortUrl);
        String longUrl = HttpUtil.get(shortUrl);
        log.info("长链:"+longUrl);

        int beginIndex = longUrl.indexOf("object_id")+10;
        int endIndex = longUrl.indexOf("&", beginIndex);
        String encrypted_data = longUrl.substring(beginIndex, endIndex);
        log.info("encrypted_data:"+encrypted_data);

        HttpRequest request = HttpUtil.createPost("https://open.douyin.com/goodlife/v1/fulfilment/certificate/prepare/?encrypted_data="+encrypted_data);
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("access-token", getAccessToken());
        request.addHeaders(headers);
        log.info(headers.toString());
        String str = request.timeout(1000).execute().body();
        log.info(str);
        return str;
    }catch (Exception e){
        e.printStackTrace();
    }
    return null;
}

四、查询商品信息

public static String queryPoi(String account_id){
    try {
        HttpRequest request = HttpUtil.createGet("https://open.douyin.com/goodlife/v1/shop/poi/query/?account_id="+account_id);
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("access-token", getAccessToken());
        request.addHeaders(headers);
//            log.info(headers.toString());
        String str = request.timeout(1000).execute().body();
        log.info(str);
        return str;
    }catch (Exception e){
        e.printStackTrace();
    }
    return null;
}

五、验券

public static String certificate(String verify_token, String encrypted_codes, String poi_id){
    try {
        HttpRequest request = HttpUtil.createPost("https://open.douyin.com/goodlife/v1/fulfilment/certificate/verify/");
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("access-token", getAccessToken());
        request.addHeaders(headers);
        log.info(headers.toString());

        JSONObject body = new JSONObject();
        body.put("verify_token", verify_token);
        body.put("poi_id", poi_id);
        JSONArray array = new JSONArray();
        array.add(encrypted_codes);
        body.put("encrypted_codes", array);
        log.info(body.toJSONString());

        String str = request.body(body.toJSONString()).timeout(2000).execute().body();
        log.info(str);
        return str;
    }catch (Exception e){
        e.printStackTrace();
    }
    return null;
}

六、参考

觉得好,就一键三连呗(点赞+收藏+关注)


原文地址:https://blog.csdn.net/kangweijian/article/details/143948588

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!