java集成拼多多开放平台且订单绑定自有平台用户代码实现(保证可用)

本文详细介绍了如何使用Java集成拼多多开放平台,完成订单与自有平台用户的绑定。从注册、申请API到绑定多多进宝账号,再到用户下单绑定流程,提供关键代码实现,包括获取access_token和授权链接的生成。适用于需要实现实名认证和订单跟踪的开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关联文章
java集成多多进宝且订单绑定自有平台用户代码实现(保证可用)
java集成淘宝联盟且订单绑定自有平台用户代码实现(保证可用)
java集成京东联盟且订单绑定自有平台用户代码实现(保证可用)

拼多多开放平台:https://open.pinduoduo.com/application/home
开放平台API文档:https://open.pinduoduo.com/application/document/api?id=pdd.ddk.goods.pid.generate
开发者文档:https://open.pinduoduo.com/application/document/browse?idStr=04DD98845AD2977D
多多进宝:https://jinbao.pinduoduo.com/

1:首先注册拼多多开放平台并申请多多客api(不多做说明)

2:登录拼多多开放平台并点击控制台

在这里插入图片描述
在这里插入图片描述
申请成功后点击查看详情就会跳转到下面这个页面,回调地址可以随便填,后面可以改的这里暂时用不到回调,当然也会说这一块
在这里插入图片描述

3:绑定多多进宝账号

在这里插入图片描述
注册一个多多进宝账号,必须要实名认证(app实名认证)。完成后点击开发者中心将拼多多开放联盟的clent_id进行绑定(这是一对一绑定的,千万不要弄错账号了,不然会很麻烦,我就是弄错账号了无法绑定其他多多进宝账号,后面只能注销绑定的多多进宝账号才能再次绑定,切记很麻烦)
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
这里的PID需要存起来很重要后面会用到

用户下单绑定流程

自由平台用户点击购买 ——> 代码判断用户是否授权(用户表字段中设一个字段) ——>
查询是否绑定推广位授权备案:
已授权:下一步
未授权:获取自有平台的用户id将用户id和推广位的pid进行备案授权(没有备案授权的话商品推广转链时是没有办法传入自定义参数的)
——> 授权成功则生成推广链接(1:按商品链接生成推广链接 2:按商品新id生成推广链接)均可传入自定义参数,前提是要有授权的用户id。如已备案的uid为1则可以添加自定义参数{“uid”:”1”,”key1”:”value1”,”key2”:”value2”…}
——> 用户下单后订单列表有一个字段custom_parameters里面就是之前加入的参数会原样返回

代码实现

pom.xml引入你下载的sdk

在这里插入图片描述

PDDUtils

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pdd.pop.sdk.common.util.JsonUtil;
import com.pdd.pop.sdk.http.PopClient;
import com.pdd.pop.sdk.http.PopHttpClient;
import com.pdd.pop.sdk.http.api.pop.request.*;
import com.pdd.pop.sdk.http.api.pop.response.*;
import com.zbkj.common.vo.exportExcelVo.PDDUnionExportParamVo;
import com.zbkj.common.vo.exportExcelVo.PDDUnionExportVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
public class PDDUtils {
    private static final Logger logger = LoggerFactory.getLogger(PDDUtils.class);

    /**
     * 淘宝客应用 id
     */
    private static final String clientId = "xxxxxxxx";
    /**
     * 淘宝客应用秘钥
     */
    private static final String clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxx";

    /**
     * 多多进宝推广位pid
     */
    private static final String pid = "xxxxxxxx_xxxxxxxx";

/**
 * pdd转链接口1 根据商品链接 https://open.pinduoduo.com/application/document/api?id=pdd.ddk.goods.zs.unit.url.gen
 * @param userId
 * @param url
 * @return
 * @throws Exception
 */
public static String pddUnionRequest(Integer userId, String url, Integer productId) throws Exception {
    PopClient client = new PopHttpClient(clientId, clientSecret);
    PddDdkGoodsZsUnitUrlGenRequest request = new PddDdkGoodsZsUnitUrlGenRequest();
    //授权成功uid后就可以带入其他参数了  但是uid必须是备案过的  https://open.pinduoduo.com/application/document/api?id=pdd.ddk.oauth.goods.zs.unit.url.gen
    request.setCustomParameters("{\"uid\":\""+userId+"\",\"productId\":\""+productId+"\"}");
    request.setGenerateShortLink(true);
    request.setPid(pid);
    request.setSourceUrl(url);
    PddDdkGoodsZsUnitUrlGenResponse response = client.syncInvoke(request);
    return JsonUtil.transferToJson(response);
}

/**
 * pdd转链接口2 根据新商品id https://open.pinduoduo.com/application/document/api?id=pdd.ddk.goods.promotion.url.generate
 * @param userId
 * @return
 * @throws Exception
 */
public static String pddUnionRequest2(Integer userId, String goodsSign, Integer productId) throws Exception {
    PopClient client = new PopHttpClient(clientId, clientSecret);
    PddDdkGoodsPromotionUrlGenerateRequest request = new PddDdkGoodsPromotionUrlGenerateRequest();
    //授权成功uid后就可以带入其他参数了  但是uid必须是备案过的  https://open.pinduoduo.com/application/document/api?id=pdd.ddk.oauth.goods.zs.unit.url.gen
    request.setCustomParameters("{\"uid\":\""+userId+"\",\"productId\":\""+productId+"\"}");
    request.setGenerateShortLink(true);
    request.setPId(pid);
    List<PddDdkGoodsPromotionUrlGenerateRequest.GoodsGenUrlParamListItem> goodsGenUrlParamList = new ArrayList<PddDdkGoodsPromotionUrlGenerateRequest.GoodsGenUrlParamListItem>();
    PddDdkGoodsPromotionUrlGenerateRequest.GoodsGenUrlParamListItem item = new PddDdkGoodsPromotionUrlGenerateRequest.GoodsGenUrlParamListItem();
    item.setGoodsSign(goodsSign);
    goodsGenUrlParamList.add(item);
    PddDdkGoodsPromotionUrlGenerateResponse response = client.syncInvoke(request);
    return JsonUtil.transferToJson(response);
}
/**
 * 获取pdd授权备案链接 https://open.pinduoduo.com/application/document/api?id=pdd.ddk.rp.prom.url.generate
 * @param userId
 * @return
 * @throws Exception
 */
public static String getAuthorization(Integer userId) throws Exception {
    PopClient client = new PopHttpClient(clientId, clientSecret);
    PddDdkRpPromUrlGenerateRequest request = new PddDdkRpPromUrlGenerateRequest();
    request.setChannelType(10);
    //备案uid 如果没有备案的话则会提示未授权
    request.setCustomParameters("{\"uid\":\""+userId+"\"}");
    List<String> pIdList = new ArrayList<String>();
    pIdList.add(pid);
    request.setPIdList(pIdList);

    PddDdkRpPromUrlGenerateResponse response = client.syncInvoke(request);
    return JsonUtil.transferToJson(response);
}

/**
 * 查询是否绑定备案 https://open.pinduoduo.com/application/document/api?id=pdd.ddk.member.authority.query
 * @param userId
 * @return {"authority_query_response":{"bind":1}}
 * @throws Exception
 */
public static String getIsBind(Integer userId) throws Exception {
    PopClient client = new PopHttpClient(clientId, clientSecret);
    PddDdkMemberAuthorityQueryRequest request = new PddDdkMemberAuthorityQueryRequest();
    //查询uid是否和pid绑定
    request.setCustomParameters("{\"uid\":\""+userId+"\"}");
    request.setPid(pid);
    PddDdkMemberAuthorityQueryResponse response = client.syncInvoke(request);
    System.out.println(JsonUtil.transferToJson(response));
    return JsonUtil.transferToJson(response);
}
/**
     * 多多进宝商品查询 https://open.pinduoduo.com/application/document/api?id=pdd.ddk.goods.search
     * @return
     * @throws Exception
     */
    public static List<PDDUnionExportVo> getGoodsSearch(PDDUnionExportParamVo pddUnionExportParamVo) throws Exception {
        PopClient client = new PopHttpClient(clientId, clientSecret);
        List<PDDUnionExportVo> pddUnionProductList = new ArrayList<>();
        String listId=null;
        for(int i=0;i<pddUnionExportParamVo.getPage();i++) {
            Integer pageNos=i;
            pageNos=pageNos+1;
            PddDdkGoodsSearchRequest request = new PddDdkGoodsSearchRequest();
            //4-秒杀,7-百亿补贴,10851-千万补贴,11879-千万神券,10913-招商礼金商品,31-品牌黑标,10564-精选爆品-官方直推爆款,10584-精选爆品-团长推荐,24-品牌高佣
//        List<Integer> activityTags = new ArrayList<Integer>();
//        activityTags.add(0);
//        request.setActivityTags(activityTags);
            //屏蔽商品类目包:1-拼多多小程序屏蔽的类目&关键词;2-虚拟类目;3-医疗器械;4-处方药;5-非处方药;6-冬奥元素相关商品
//        List<Integer> blockCatPackages = new ArrayList<Integer>();
//        blockCatPackages.add(0);
//        request.setBlockCatPackages(blockCatPackages);
            //自定义屏蔽一级/二级/三级类目ID,自定义数量不超过20个;使用pdd.goods.cats.get接口获取cat_id
//        List<Integer> blockCats = new ArrayList<Integer>();
//        blockCats.add(0);
//        request.setBlockCats(blockCats);
            //商品类目ID,使用pdd.goods.cats.get接口获取
            if(pddUnionExportParamVo.getCatId() != null) {
                request.setCatId(pddUnionExportParamVo.getCatId());
            }
            //商品主图类型:1-场景图,2-白底图,默认为0
//        request.setGoodsImgType(1);
            //是否为品牌商品
            request.setIsBrandGoods(true);
            if(StringUtils.isNotBlank(pddUnionExportParamVo.getKeyword())) {
                //商品关键词,与opt_id字段选填一个或全部填写。可支持goods_id、拼多多链接(即拼多多app商详的链接)、进宝长链/短链(即为pdd.ddk.goods.promotion.url.generate接口生成的长短链)
                request.setKeyword(pddUnionExportParamVo.getKeyword());
            }
            //店铺类型,1-个人,2-企业,3-旗舰店,4-专卖店,5-专营店,6-普通店(未传为全部)
//        request.setMerchantType(1);
            //店铺类型数组,例如:[1,2]
//        List<Integer> merchantTypeList = new ArrayList<Integer>();
//        merchantTypeList.add(0);
//        request.setMerchantTypeList(merchantTypeList);
            //商品标签类目ID,使用pdd.goods.opt.get获取
//        request.setOptId(49L);
            //默认值1,商品分页数
            request.setPage(pageNos);
            //默认100,每页商品数量
            request.setPageSize(100);
            //推广位id
            request.setPid(pid);
            //翻页时建议填写前页返回的list_id值
            request.setListId(listId);

//        List<PddDdkGoodsSearchRequest.RangeListItem> rangeList = new ArrayList<PddDdkGoodsSearchRequest.RangeListItem>();
//
//        PddDdkGoodsSearchRequest.RangeListItem item = new PddDdkGoodsSearchRequest.RangeListItem();
//        //区间的开始值
//        item.setRangeFrom(0L);
//        //0,最小成团价 1,券后价 2,佣金比例 3,优惠券价格 4,广告创建时间 5,销量 6,佣金金额 7,店铺描述分 8,店铺物流分 9,店铺服务分 10, 店铺描述分击败同行业百分比 11, 店铺物流分击败同行业百分比 12,店铺服务分击败同行业百分比 13,商品分 17 ,优惠券/最小团购价 18,过去两小时pv 19,过去两小时销量
//        item.setRangeId(0);
//        //区间的结束值
//        item.setRangeTo(0L);
//        rangeList.add(item);
//        request.setRangeList(rangeList);
            //排序方式:0-综合排序;1-按佣金比率升序;2-按佣金比例降序;3-按价格升序;4-按价格降序;5-按销量升序;6-按销量降序;7-优惠券金额排序升序;8-优惠券金额排序降序;9-券后价升序排序;10-券后价降序排序;11-按照加入多多进宝时间升序;12-按照加入多多进宝时间降序;13-按佣金金额升序排序;14-按佣金金额降序排序;15-店铺描述评分升序;16-店铺描述评分降序;17-店铺物流评分升序;18-店铺物流评分降序;19-店铺服务评分升序;20-店铺服务评分降序;27-描述评分击败同类店铺百分比升序,28-描述评分击败同类店铺百分比降序,29-物流评分击败同类店铺百分比升序,30-物流评分击败同类店铺百分比降序,31-服务评分击败同类店铺百分比升序,32-服务评分击败同类店铺百分比降序
            request.setSortType(6);
            //是否使用个性化推荐,true表示使用,false表示不使用,默认true。
//        request.setUseCustomized(true);
            //是否只返回优惠券的商品,false返回所有商品,true只返回有优惠券的商品
            request.setWithCoupon(false);
            PddDdkGoodsSearchResponse response = client.syncInvoke(request);

            String goodsSearch=JsonUtil.transferToJson(response);
            JSONObject json = JSONObject.parseObject(goodsSearch);
            if(json.containsKey("goods_search_response")){
                json=JSONObject.parseObject(json.getString("goods_search_response"));
                if(json.containsKey("goods_list")) {
                    JSONArray jsonArray = JSONArray.parseArray(json.getString("goods_list"));
                    for (int j = 0; j < jsonArray.size(); j++) {
                        JSONObject jsonObject = JSONObject.parseObject(jsonArray.getString(j));
                        PDDUnionExportVo pddUnionExportVo = new PDDUnionExportVo();
                        pddUnionExportVo.setBrandName(jsonObject.getString("brand_name"));
                        pddUnionExportVo.setMallName(jsonObject.getString("mall_name"));
                        pddUnionExportVo.setMerchantType(jsonObject.getInteger("merchant_type"));
                        pddUnionExportVo.setGoodsSign(jsonObject.getString("goods_sign"));
                        pddUnionExportVo.setTitle(jsonObject.getString("goods_name"));
                        pddUnionExportVo.setHasCoupon(jsonObject.getBoolean("has_coupon"));
                        pddUnionExportVo.setSalesTip(jsonObject.getString("sales_tip"));
                        pddUnionExportVo.setMinGroupPrice(jsonObject.getLong("min_group_price"));
                        pddUnionExportVo.setMinNormalPrice(jsonObject.getLong("min_normal_price"));
                        pddUnionExportVo.setPromotionRate(jsonObject.getLong("promotion_rate"));
                        pddUnionProductList.add(pddUnionExportVo);
                    }
                }
                listId=json.getString("list_id");
            }
        }

        return pddUnionProductList;
    }

    /**
     * 查询商品详情根据新商品id https://open.pinduoduo.com/application/document/api?id=pdd.ddk.goods.detail
     * @param goodsSign
     * @return
     * @throws Exception
     */
    public static String getGoodsDetail(String goodsSign) throws Exception {
        PopClient client = new PopHttpClient(clientId, clientSecret);

        PddDdkGoodsDetailRequest request = new PddDdkGoodsDetailRequest();
        request.setGoodsSign("str");
        request.setPid(pid);
        PddDdkGoodsDetailResponse response = client.syncInvoke(request);
        return JsonUtil.transferToJson(response);
    }
/**
     * 查询订单列表 https://open.pinduoduo.com/application/document/api?id=pdd.ddk.all.order.list.increment.get
     * @return
     * @throws Exception
     */
    public static String getPDDOrderList(Long startTime,Long endTime,Integer PageNo) throws Exception {
        PopClient client = new PopHttpClient(clientId, clientSecret);
        PddDdkAllOrderListIncrementGetRequest request = new PddDdkAllOrderListIncrementGetRequest();
        //查询结束时间,和开始时间相差不能超过24小时。note:此时间为时间戳,指格林威治时间 1970 年01 月 01 日 00 时 00 分 00 秒(北京时间 1970 年 01 月 01 日 08 时 00 分 00 秒)起至现在的总秒数
        request.setEndUpdateTime(endTime);
        //第几页,从1到10000,默认1,注:使用最后更新时间范围增量同步时,必须采用倒序的分页方式(从最后一页往回取)才能避免漏单问题。
        request.setPage(PageNo);
        //返回的每页结果订单数,默认为100,范围为10到100,建议使用40~50,可以提高成功率,减少超时数量。
        request.setPageSize(40);
        //订单类型:1-推广订单;2-直播间订单
        request.setQueryOrderType(1);
        //最近90天内多多进宝商品订单更新时间--查询时间开始
        request.setStartUpdateTime(startTime);
        PddDdkAllOrderListIncrementGetResponse response = client.syncInvoke(request);
        return JsonUtil.transferToJson(response);
    }
}

PDDUnionExportVo

@Data
@ApiModel(value="PDDUnionExportVo", description="拼多多参数导出对象")
public class PDDUnionExportVo {
    //品牌名
    private String brandName;
    //店铺名称
    private String mallName;
   //店铺类型,1-个人,2-企业,3-旗舰店,4-专卖店,5-专营店,6-普通店
    private Integer merchantType;
    //新商品id
    private String goodsSign;
    //商品标题
    private String title;
    //商品是否有优惠券 true-有,false-没有
    private Boolean hasCoupon;
    //已售卖件数
    private String salesTip;
    //最小拼团价(单位为分)
    private Long minGroupPrice;
    //最小单买价格(单位为分)
    private Long minNormalPrice;
    //佣金比例,千分比
    private Long promotionRate;
}

在这里插入图片描述

获取access_token

在这里插入图片描述
这里有一个回调地址,回调地址里面会返回一个code以及一个state自定义参数。回调地址不要有权限校验,get请求需要能在浏览器直接访问

授权文档:https://open.pinduoduo.com/application/document/browse?idStr=BD3A776A4D41D5F5
在这里插入图片描述
这里使用的是多多进宝推手(具体视项目而定)

返回拼多多授权链接:授权需要多多进宝且实名制的账号才可以授权

private static final String LOCAL_CALLBACK_URL = "http://xxxxxx.xxx/api/front/tbUnion/taobaoCallback";
    private static final String CALLBACK_URL = "https://xxxxxx.xxx/front/api/front/tbUnion/taobaoCallback";

    /**
     * 返回拼多多授权链接
     * @return
     */
    public static String pddAuthorizationUrl(String userId,String source,String id){
        String views="h5";
        if("pc".equals(source)){
            views="web";
        }
//        String result="https://jinbao.pinduoduo.com/open.html?response_type=code&client_id="+clientId+"&redirect_uri="+CALLBACK_URL+"&state="+userId+"-"+source+"-"+id+"&view="+views;
        String result="https://jinbao.pinduoduo.com/open.html?response_type=code&client_id="+clientId+"&redirect_uri="+LOCAL_CALLBACK_URL+"&state="+userId+"-"+source+"-"+id+"&view="+views;
        return result;
    }

回调示例:能获取到token和多多客的id(owner_id)

@Value("${taobao.jump.url}")
private String jumpH5Url;

@ApiOperation(value = "拼多多回调")
@RequestMapping(value = "/pddCallback", method = RequestMethod.GET)
public String pddCallback(HttpServletRequest request) {
    // 获取code参数
    String code = request.getParameter("code");
    String state = request.getParameter("state");
    String[] split = state.split("-");
    Integer userId = Integer.valueOf(split[0]);
    String source = split[1];
    String id = split[2];
    if (userId != null) {
        try {
            //获取用户授权的accessToken
            String token = PDDUtils.getToken(code);
            JSONObject json = JSONObject.parseObject(token);
            JSONObject authToken = JSONObject.parseObject(json.getString("pop_auth_token_create_response"));
            String accessToken = authToken.getString("access_token");
            String ownerId = authToken.getString("owner_id");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return "redirect:" + jumpH5Url + "?source=" + source + "&id=" + id + "&type=success";
}

Code获取token:很多接口需要token的都可以根据这种方式获取

/**
 * 多多进宝根据code获取access_token
 * @param code
 * @return
 * @throws ApiException
 */
public static String getToken(String code) throws Exception {
    PopClient client = new PopHttpClient(clientId, clientSecret);
    PddPopAuthTokenCreateRequest request = new PddPopAuthTokenCreateRequest();
    request.setCode(code);
    PddPopAuthTokenCreateResponse response = client.syncInvoke(request);
    return JsonUtil.transferToJson(response);
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一名落魄的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值