C # Taobao merchandise WeChat rebate assistant development-(5) Rebate assistant development (3) How to convert Taobao password to link through API

A series of tutorials: the principle of rebate assistant

Series 2 of tutorial series: Rebate Assistant Open Documents and Account Application Address

Three directories in the series of tutorials: Rebate Assistant Development (1) API Introduction

Four directories in the series of tutorials: Rebate Assistant Development (2) How to get the Taobao password for the content shared by Taobao

Five directories in the series of tutorials: Rebate Assistant Development (3) How to convert passwords to links through API

Series 6 of tutorial series: Rebate Assistant Development (4) If you obtain rebate information through the address resolved by the Taobao password

Seven directories in the series of tutorials: Rebate Assistant Development (5) How to Convert Coupon Address to Amoy Password

Eight Courses in Series: Docking of WeChat

A series of tutorial nine directories: write a vue page for copying the password

#Through sharing, we know how to get only the value of the Taobao password through this method. At this time, how do we parse the link of the product through the Taobao password?
API documentation
In fact, Taobao has an API to convert the Taobao password into a link and get the product. ID
Amoy password API.png
before beginning to write I did not join the public API parameters during the preparation process leading to the beginning of the API request has been behind the failure, although this chapter is to explain taobao.tbk.tpwd.convert this interface API, but all had to pass this common parameters Content, so in this chapter I feel that I will first explain the encapsulation of public parameters for later quick calls

Public parameter.png
Expanding the public parameters, we found that there are a lot of things in it. Is it possible that some people are getting bigger, I will organize the contents here,
encapsulate an entity class to store some fixed things here, I have assigned values

  public class initdata
    {
        public String method { get; set; }
        public String app_key { get; set; }
        public String target_app_key { get; set; }
        public String sign_method { get; set; } = "md5";
        public String session { get; set; }
        public String timestamp { get; set; } = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", DateTimeFormatInfo.InvariantInfo);
        public String format { get; set; } = "json";
        public String v { get; set; } = "2.0";
        public String partner_id { get; set; } = "top-apitools";
        public Boolean simplify { get; set; }
    }

I have written here and there are careful friends who have already learned the law. There is a must-have content here. I did not write it. This is the sign field.
Why did I not write the sign field? Because we look at the sign algorithm to introduce the
Sign.png
sign. This field requires public parameters. And business parameters, all sorted by ASCII code
and then spliced, the spliced ​​value is then encrypted with md5 and then hmac password

Since this is troublesome every time, we will encapsulate the method

 public class AppUtil
    {
        /// <summary>
        /// 将参数排序组装
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static string BuildParamStr(Dictionary<string, string> param)
        {
            if (param == null || param.Count == 0)
            {
                return "";
            }
            Dictionary<string, string> ascDic = param.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
            StringBuilder sb = new StringBuilder();
            foreach (var item in ascDic)
            {
                if (!string.IsNullOrEmpty(item.Value))
                {
                    sb.Append(item.Key).Append("=").Append(item.Value).Append("&");
                }
                
            }

            return sb.ToString().Substring(0,sb.ToString().Length-1);
        }

        public static string signParam(Dictionary<string, string> param, string appkey)
        {
            if (param == null || param.Count == 0)
            {
                return "";
            }
            param.Add("key", appkey);
            string blankStr = BuildParamStr(param);
            return MD5Encrypt(blankStr);

        }


        /// <summary>
        /// 将实体转化为json
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static string ObjectToJson(object o)
        {
            string json = JsonConvert.SerializeObject(o);
            return json;
        }


        /// <summary>
        /// md5加签
        /// </summary>
        /// <param name="strText"></param>
        /// <returns></returns>
        public static string MD5Encrypt(string strText)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] result = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strText));
            // 第四步:把二进制转化为大写的十六进制
            StringBuilder results = new StringBuilder();
            for (int i = 0; i < result.Length; i++)
            {
                results.Append(result[i].ToString("X2"));
            }

            return results.ToString();
        }
        public static string SignTopRequest(IDictionary<string, string> parameters, string secret, string signMethod)
        {
            // 第一步:把字典按Key的字母顺序排序
            IDictionary<string, string> sortedParams = new SortedDictionary<string, string>(parameters, StringComparer.Ordinal);
            IEnumerator<KeyValuePair<string, string>> dem = sortedParams.GetEnumerator();

            // 第二步:把所有参数名和参数值串在一起
            StringBuilder query = new StringBuilder();
            if (Constants.SIGN_METHOD_MD5.Equals(signMethod))
            {
                query.Append(secret);
            }
            while (dem.MoveNext())
            {
                string key = dem.Current.Key;
                string value = dem.Current.Value;
                if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
                {
                    query.Append(key).Append(value);
                }
            }

            // 第三步:使用MD5/HMAC加密
            byte[] bytes;
            if (Constants.SIGN_METHOD_HMAC.Equals(signMethod))
            {
                HMACMD5 hmac = new HMACMD5(Encoding.UTF8.GetBytes(secret));
                bytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(query.ToString()));
            }
            else
            {
                query.Append(secret);
                MD5 md5 = MD5.Create();
                bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(query.ToString()));
            }

            // 第四步:把二进制转化为大写的十六进制
            StringBuilder result = new StringBuilder();
            for (int i = 0; i < bytes.Length; i++)
            {
                result.Append(bytes[i].ToString("X2"));
            }

            return result.ToString();
        }

       
    }

In this way, we can get the sign value by calling the SignTopRequest method of AppUtil. Maybe it is a bit troublesome to write the public repeatedly every time. Let ’s encapsulate it further.

  private Dictionary<string, string> buildBasicParam(initdata rsp)
        {
            Dictionary<string, string> param = new Dictionary<string, string>();
            if (!String.IsNullOrEmpty(rsp.method))
            {
                param.Add("method", rsp.method);
            }
            if (!String.IsNullOrEmpty(rsp.app_key))
            {
                param.Add("app_key", rsp.app_key);
            }

            if (!String.IsNullOrEmpty(rsp.sign_method))
            {
                param.Add("sign_method", rsp.sign_method);
            }

            if (!String.IsNullOrEmpty(rsp.session))
            {
                param.Add("session", rsp.session);
            }
            if (!String.IsNullOrEmpty(rsp.timestamp))
            {
                param.Add("timestamp", rsp.timestamp);
            }
            if (!String.IsNullOrEmpty(rsp.format))
            {
                param.Add("format", rsp.format);
            }
            if (!String.IsNullOrEmpty(rsp.v))
            {
                param.Add("v", rsp.v);
            }
           
            return param;
        }
        string appkey = SiteConfig.GetSite("appkey");
        string appsecret = SiteConfig.GetSite("appsecret");
        string sessionkey = SiteConfig.GetSite("sessionkey");
        string adzone_id = SiteConfig.GetSite("adzone_id");
        string site_id = SiteConfig.GetSite("site_id");
        string usertoken = SiteConfig.GetSite("usertoken");
        string Taobaourl = SiteConfig.GetSite("Taobaourl");

       public string AutoUrl(string method, Dictionary<string, string> adddic)
        {

            initdata rsp = new initdata();
            rsp.method = method;
            rsp.app_key = appkey;
            rsp.session = sessionkey;
            rsp.sign_method = "hmac";
            Dictionary<string, string> paramDic = buildBasicParam(rsp);
            paramDic = paramDic.Concat(adddic).ToDictionary(k => k.Key, v => v.Value);
            paramDic.Add("sign", AppUtil.SignTopRequest(paramDic, appsecret, "hmac"));
            WebUtils utils = new WebUtils();
            string  shorturl = utils.DoPost(Taobaourl, paramDic);
         
            return shorturl;
        }

After encapsulation, we only need to call AutoUrl to pass this business parameter of each interface, and we can quickly complete the access to each interface.

As for this interface, we can achieve access to the API through such a short code

     Dictionary<string, string> param = new Dictionary<string, string>();
     param.Add("password_content", password_content);
     param.Add("adzone_id", adzone_id);
     string  Url= taoserver.AutoUrl("taobao.tbk.tpwd.convert", param);
Published 29 original articles · Like 11 · Visits 10,000+

Guess you like

Origin blog.csdn.net/u010840685/article/details/105133905