如何构建阿里API请求数据

使用阿里API前,首先要用企业账号申请开发者,并创建应用,授权用户。这些都在平台说明里有。阿里平台介入说明

这里主要说一下,如何构建阿里API请求数据。以

com.alibaba.trade:alibaba.trade.getBuyerOrderList-1 订单列表查看(买家视角)

为例

Controller

        /// <summary>
        /// 获取1688用户所有未付款订单
        /// </summary>
        /// <param name="accountId">下单账户</param>
        /// <returns></returns>
        public JsonResult GetOrderList(string accountId)
        {
            AliAccountModel model = aliAccountService.GetAliAccount(GlobalModel.Instance.AliAPIAppName, accountId);
            model.API = "com.alibaba.trade/alibaba.trade.getBuyerOrderList";//需要的API:获取用户订单列表
            model.Parameters = "page=1&pageSize=20&needBuyerAddressAndPhone=false&needMemoInfo=false";//参数第一页前20条数据

            #region 时间戳计算:需要考虑阿里平台时间
            //System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
            //long timeStamp = (long)(DateTime.Now - startTime).TotalMilliseconds; // 相差毫秒数 
            //model.TimeStamp = timeStamp.ToString();
            #endregion

            string strUrl = aliAccountService.GetAliRequestUrl(model);

            return Json(new { Status = true, Msg = strUrl });
        }

实现

/// <summary>
        /// 获取1688请求url
        /// </summary>
        /// <param name="model">接口实体</param>
        /// <returns></returns>
        public string GetAliRequestUrl(AliAccountModel model)
        {
            string strUrl = string.Format(@"param2/1/{0}/{1}", model.API, model.APPKey);
            string strParameter = string.Empty;//参数
            string strJoinParameter = string.Empty;//参数拼装处理


            #region 参数:strParameter;拼装参数:strJoinParameter

            List<string> list = new List<string>();
            strParameter = model.Parameters;
            strParameter += "&access_token=" + model.AccessToken;
            if (!string.IsNullOrWhiteSpace(model.TimeStamp))
            {
                strParameter += "&_aop_timestamp=" + model.TimeStamp;
            }

            string[] arrayParameter = strParameter.Split(new[] { '&' }, StringSplitOptions.None);

            foreach (string item in arrayParameter)
            {
                list.Add(item.Replace("=", ""));
            }

            list.Sort();

            strJoinParameter = string.Join("", list);

            #endregion

            #region sha1计算结果:hex

            string strSha1 = strUrl + strJoinParameter;
            var SecretKey = Encoding.Default.GetBytes(model.APPSecretKey);
            byte[] data = Encoding.Default.GetBytes(strSha1);

            System.Security.Cryptography.HMACSHA1 hmac = new System.Security.Cryptography.HMACSHA1() { Key = SecretKey };
            var result = hmac.ComputeHash(data);

            string hex = BitConverter.ToString(result, 0).Replace("-", string.Empty).ToUpper();

            #endregion

            strParameter += "&_aop_signature=" + hex;
            strUrl += strParameter;
            strUrl = System.Web.HttpUtility.UrlEncode(strUrl);

            return strUrl;
        }


猜你喜欢

转载自blog.csdn.net/spw55381155/article/details/80434607