Esempio n. 1
0
        /// <summary>
        /// 获取AccessToken
        /// </summary>
        /// <returns></returns>
        public static string GetAccessToken()
        {
            QyAccessToken accessToken = null;
            bool          isGet       = false;

            if (CacheUtil.Exists("QyAccessToken"))
            {
                accessToken = CacheUtil.Get <QyAccessToken>("QyAccessToken");
                if (accessToken.Expire_Time >= DateTime.Now.AddMinutes(1))
                {
                    isGet = true;
                }
            }
            if (!isGet)
            {
                string url = $"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={CorpID}&corpsecret={Secret}";
                //请求获取
                string res = RequestUtil.HttpGet(url);
                accessToken = JsonConvert.DeserializeObject <QyAccessToken>(res);
                if (accessToken != null && accessToken.ErrCode == 0)
                {
                    accessToken.Expire_Time = DateTime.Now.AddSeconds(accessToken.Expires_In);
                    CacheUtil.Set("QyAccessToken", accessToken, new TimeSpan(2, 0, 0));
                }
                else
                {
                    LogUtil.Error($"获取accesstoken失败——{accessToken.ErrCode},{accessToken.ErrMsg}");
                }
            }

            return(accessToken.Access_Token);
        }