Ejemplo n.º 1
0
 /// <summary>
 /// 获取可用 accessToken
 /// </summary>
 /// <param name="model"></param>
 /// <returns></returns>
 private static string GetNewAccessToken(HkInterfaceModel model)
 {
     try
     {
         string paramsValue = string.Format("appKey={0}&appSecret={1}", model.Appkey, model.Secret);
         var    response    = HttpHelper.Post(model.AccessTokenUrl, paramsValue);
         var    data        = JsonConvert.DeserializeObject <PackageData <AccessTokenModel> >(response);
         if (data != null)
         {
             if (data.code == ReturnCode.Success)
             {
                 if (data.data != null && !string.IsNullOrWhiteSpace(data.data.accessToken))
                 {
                     UpdateAccessToken(data.data.accessToken);
                     return(data.data.accessToken);
                 }
                 else
                 {
                     throw new Exception("获取接口 accessToken 失败!");
                 }
             }
             else
             {
                 throw new Exception(string.Format("接口错误:{0}-{1}", data.code, data.msg));
             }
         }
         else
         {
             throw new Exception("获取接口 accessToken 失败!");
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 获取接口相关配置参数(包含 accessToken)
        /// </summary>
        /// <returns></returns>
        private static Result <HkInterfaceModel> GetAccessToken()
        {
            Result <HkInterfaceModel> result = new Result <HkInterfaceModel>();

            try
            {
                List <string> keys = new List <string>()
                {
                    Settings.HkAppKey.ToString(),
                              Settings.HkSecret.ToString(),
                              Settings.HkAccessToken.ToString(),
                              Settings.HkGetAccessTokenUrl.ToString(),
                              Settings.HkOpenVideoUrl.ToString(),
                              Settings.HkVideoUrl.ToString(),
                              Settings.HkAddVideoUrl.ToString(),
                              Settings.HkEditVideoUrl.ToString(),
                              Settings.HkDeleteVideoUrl.ToString()
                };

                var dic = DataOperateBasic <Base_Settings> .Get().GetList(p => keys.Contains(p.Code)).ToDictionary(p => p.Code, p => p.Value);

                if (dic == null || !dic.Any())
                {
                    throw new Exception("未配置设备相关参数接口!");
                }

                if (!dic.ContainsKey(Settings.HkAppKey.ToString()) ||
                    !dic.ContainsKey(Settings.HkSecret.ToString()) ||
                    !dic.ContainsKey(Settings.HkGetAccessTokenUrl.ToString()) ||
                    !dic.ContainsKey(Settings.HkOpenVideoUrl.ToString()) ||
                    !dic.ContainsKey(Settings.HkVideoUrl.ToString()) ||
                    !dic.ContainsKey(Settings.HkAddVideoUrl.ToString()) ||
                    !dic.ContainsKey(Settings.HkEditVideoUrl.ToString()) ||
                    !dic.ContainsKey(Settings.HkDeleteVideoUrl.ToString()))
                {
                    throw new Exception("未配置设备相关参数接口!");
                }

                HkInterfaceModel model = new HkInterfaceModel();
                model.Appkey         = dic[Settings.HkAppKey.ToString()];
                model.Secret         = dic[Settings.HkSecret.ToString()];
                model.AccessToken    = dic[Settings.HkAccessToken.ToString()];
                model.AccessTokenUrl = dic[Settings.HkGetAccessTokenUrl.ToString()];
                model.OpenVideoUrl   = dic[Settings.HkOpenVideoUrl.ToString()];
                model.VideoUrl       = dic[Settings.HkVideoUrl.ToString()];
                model.AddVideoUrl    = dic[Settings.HkAddVideoUrl.ToString()];
                model.EditVideoUrl   = dic[Settings.HkEditVideoUrl.ToString()];
                model.DeleteVideoUrl = dic[Settings.HkDeleteVideoUrl.ToString()];

                result.Data = model;
                result.Flag = EResultFlag.Success;
            }
            catch (Exception ex)
            {
                result.Data      = null;
                result.Flag      = EResultFlag.Failure;
                result.Exception = new ExceptionEx(ex, "GetAccessToken");
            }
            {
            }
            return(result);
        }