排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:
50010702506256


欢迎加群交流技术

创建一个类库DTO_Token_Tools,在创建一个工具类
在工具类里面写一个access_Token_Tool()方法
//获取token public string access_Token_Tool() { HttpClient httpClient = new HttpClient(); RedisClient redisClient = new RedisClient(); string token = redisClient.Get<string>("access_token"); if (token != null) { return token; } else { //调用接口获取微信开发的token和过期时间 string result = httpClient.GetAsync("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=xxxxxxx&secret=xxxxxxxxxxxxxxxxx").Result.Content.ReadAsStringAsync().Result; Access_Token_DTO access_Token_DTO = JsonConvert.DeserializeObject<Access_Token_DTO>(result); //储存缓存Redis并且设置过期时间和token过期时间同步 redisClient.Set<string>("access_token", access_Token_DTO.access_token,TimeSpan.FromSeconds(Convert.ToInt32(access_Token_DTO.expires_in)-100)); return access_Token_DTO.access_token; } }
评价