【分享】跨境虾皮Shopee各区域商品详情API返回值(商品,订单,面单等)♥

本文介绍了如何在虾皮Shopee电商平台中进行授权,包括获取授权代码、生成加密签名、首次获取和刷新token的过程。开发者需使用HmacSHA256算法并存储关键参数以确保安全通信。
摘要由CSDN通过智能技术生成

虾皮(shopee)是一个亚洲区域的电商平台,主要在东南亚地区提供电商服务。虾皮提供了丰富的电商数据,包括商品数据、订单数据、会员数据、评价数据等。

虾皮Shopee♥♥​​​​​​​♥​​​​​​​♥​​​​​​​♥​​​​​​​♥

1.授权

接口文档说明了:授权需要四个参数:合作伙伴ID,回调地址,生成加密签名,时间戳。
有了这四个参数,我们要把拿到的返回参数code还有shop_id存进数据库,code很重要,是用来后面更新token用的。

2.如何生成sign

使用加密算法HmacSHA256计算

        /// 加密算法HmacSHA256
        /// </summary>
        /// <param name="secret"></param>
        /// <param name="signKey"></param>
        /// <returns></returns>
        public static string HmacSHA256(string secret, string signKey)
        {
            string signRet = string.Empty;
            using (HMACSHA256 mac = new HMACSHA256(Encoding.UTF8.GetBytes(signKey)))
            {
                byte[] hash = mac.ComputeHash(Encoding.UTF8.GetBytes(secret));
                signRet = ToHexString(hash);
            }
            return signRet;
        }
        
         /// <summary>
        /// byte[]转16进制格式string
        /// </summary>
        public static string ToHexString(byte[] bytes)
        {
            string hexString = string.Empty;
            if (bytes != null)
            {
                StringBuilder strB = new StringBuilder();
                foreach (byte b in bytes)
                {
                    strB.AppendFormat("{0:x2}", b);
                }
                hexString = strB.ToString();
            }
            return hexString;
        }

返回参数会在回调地址上,拿到返回参数后,用来生成token

3.首次生成token


        /// <summary>
        /// 首次获取Token
        /// </summary>
        /// <param name="code"></param>
        /// <param name="partner_id"></param>
        /// <param name="partner_key"></param>
        /// <param name="shop_id"></param>
        /// <returns></returns>
        public ReturnMeg get_token(string code, int partner_id, string partner_key, int shop_id)
        {
            long timest = Gettimest();
            var bodto = new
            {
                code = code,

                partner_id = partner_id,
                shop_id = shop_id,
            };
            string body = JsonConvert.SerializeObject(bodto);
            var host = System.Configuration.ConfigurationManager.AppSettings["ShpoeeUrl"];
            var path2 = "/api/v2/auth/token/get";
            var base_string = $"{partner_id}{path2}{timest}";
            var sign = HmacSHA256(base_string, partner_key);
            var url = $"{host}{path2}?sign={sign}&partner_id={partner_id}&timestamp={timest}&|";
            string sd = Tool.HttpPostJSONs(url, body);
            JObject tokendata = (JObject)JsonConvert.DeserializeObject(sd);
            if (tokendata["error"].ToString() != "")
            {
                return new ReturnMeg
                {
                    State = ReturnState.error,
                    Msg = "授权失败"
                };
            }
            Shopee s = new Shopee();

            s.Access_token = tokendata["access_token"].ToString();
            s.Refresh_token = tokendata["refresh_token"].ToString();
            int expire_in = MsParse.ToInt32(tokendata["expire_in"].ToString());//有效时间
            s.GetTokenTime = DateTime.Now.AddSeconds((expire_in - 600));


            var path = "/api/v2/shop/get_shop_info";
            var redirect_url = setup.PcUserUrl + "/SQ/Index";
            var base_strings = $"{partner_id}{path}{timest}{s.Access_token}{shop_id}";
            var signs = HmacSHA256(base_strings, partner_key);
            string strData = $"{partner_key}{redirect_url}";

            string aaa = ($"{host}{path}?shop_id={shop_id}&partner_id={partner_id}&access_token={s.Access_token}&sign={signs}&timestamp={timest}");
            string back = Tool.HttpGet(aaa);
            if (back == "")
            {
                return new ReturnMeg
                {
                    State = ReturnState.error,
                    Msg = "获取店铺信息失败"
                };
            }
            JObject joP = (JObject)JsonConvert.DeserializeObject(back);
            s.ShopName = joP["shop_name"].ToString();
            s.region = joP["region"].ToString();
            s.ShopState = joP["status"].ToString();
            s.auth_time = StampToDateTime(joP["auth_time"].ToString());
            s.expire_time = StampToDateTime(joP["expire_time"].ToString());
            s.Creationtime = DateTime.Now;
            s.shop_id = shop_id;
            s.code = code;
            Shopee s1 = shopeeIBLL.GetShopee(shop_id);
            s.Id = s1.Id;
            if (s1.Id > 0)
            {
                shopeeIBLL.UpDate(s);
            }
            else
            {
                shopeeIBLL.Add(s);
            }

            return new ReturnMeg
            {
                State = ReturnState.success,
                Msg = "授权成功"
            };
        }


        /// <summary>
        /// 用店铺Id刷新Token
        /// </summary>
        /// <param name="shop_Id"></param>
        /// <returns></returns>
        public void SxToken(int shop_Id)
        {
            Shopee s = shopeeIBLL.GetShopee(shop_Id);
            //时间不到,不用刷新
            if (s.GetTokenTime >= DateTime.Now)
            {
                return;
            }
            int partner_id = MsParse.ToInt32(System.Configuration.ConfigurationManager.AppSettings["Shpoeepartner_id"]);
            var partner_key = System.Configuration.ConfigurationManager.AppSettings["Shpoeepartner_key"];
            long timest = Gettimest();
            var bodto = new
            {
                refresh_token = s.Refresh_token,
                partner_id = partner_id,
                shop_id = s.shop_id,
                code = s.code,
            };
            string body = JsonConvert.SerializeObject(bodto);
            var host = System.Configuration.ConfigurationManager.AppSettings["ShpoeeUrl"];
            var path2 = "/api/v2/auth/access_token/get";
            var base_string = $"{partner_id}{path2}{timest}";
            var sign = HmacSHA256(base_string, partner_key);
            var url = $"{host}{path2}?sign={sign}&partner_id={partner_id}&timestamp={timest}&|";
            string sd = Tool.HttpPostJSONs(url, body);
            JObject tokendata = (JObject)JsonConvert.DeserializeObject(sd);

            s.Access_token = tokendata["access_token"].ToString();
            s.Refresh_token = tokendata["refresh_token"].ToString();
            int expire_in = MsParse.ToInt32(tokendata["expire_in"].ToString());//有效时间
            s.GetTokenTime = DateTime.Now.AddSeconds((expire_in - 600));
            shopeeIBLL.UpDate(s);
        }
       

获取到返回参数后,存入数据库。

4.刷新token

拿到首次获取token返回参数refresh_token,调用刷新token接口。
注意(之后的授权,或是对接虾皮其他接口都要先判断token是否过期,过期了就要调用刷新接口)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值