萤石云定时更新 accessToken
class UpdateVideoTokenHelper { private static Timer myTimer; private static string appKey = ConfigurationManager.AppSettings["appKey"]; private static string appSecret = ConfigurationManager.AppSettings["appSecret"]; public static void SetTimer() { myTimer = new Timer(10000); myTimer.Elapsed += OnTimedEvent; myTimer.AutoReset = true; myTimer.Enabled = true; } private static void OnTimedEvent(Object source, ElapsedEventArgs e) { long updateTime = UpdateToken(); Timer timer = source as Timer; if (updateTime > 0) { timer.Interval = updateTime; } else { timer.Interval = 432000; } } private static long UpdateToken() { try { WebClient webClient = new WebClient(); string postString = "appKey=" + appKey + "&appSecret=" + appSecret; //以form表单的形式上传 webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); // 转化成二进制数组 byte[] postData = Encoding.ASCII.GetBytes(postString); // 上传数据 byte[] responseData = webClient.UploadData("https://open.ys7.com/api/lapp/token/get", "POST", postData); string res = Encoding.UTF8.GetString(responseData); var jsonObj = JObject.Parse(JsonConvert.DeserializeObject(res).ToString()); if (jsonObj["code"].ToString() == "200") { string accessToken = jsonObj["data"]["accessToken"].ToString(); string sql = "update FM_DEVICE set EXTENDCODE5 = '" + accessToken + "'"; int excuteRes = DBHelper.ExecuteCommand(sql); }
//计算出需要更新的时间 修改timer 执行时间 long expireTime = long.Parse(jsonObj["data"]["expireTime"].ToString()); DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0, 0)); long nowTime = (DateTime.Now.Ticks - startTime.Ticks) / 10000; return expireTime - nowTime; } catch (Exception e) { Console.WriteLine(e.ToString()); return 20000; } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!