微信公众平台对接C#-AccessToken处理

1.获取新accessToken

//发起过接口token请求 token过期
                string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
                WebClient webClient = new WebClient();
                Byte[] bytes = webClient.DownloadData(string.Format("{0}&appid={1}&secret={2}", url, AppID, Secret));
                string result = Encoding.GetEncoding("utf-8").GetString(bytes);
                string[] temp = result.Split(',');
                string[] tp = temp[0].Split(':');
                T9.Util.LogUtil.WriteLog("更新token请求开始" + result, guid, LogName);
                AccessTokenModel model = new AccessTokenModel(AppID, tp[1].ToString().Replace('"', ' ').Trim().ToString(), DateTime.Now);
                CommonApi.AdddicAccessToken(model);
                T9.Util.LogUtil.WriteLog("更新token请求" + syncurl + "?otype=ChangeToken&newSupToken=" + model.BasicSupToken + "&newSupTokenTime=" + model.BasicSupToken_Time.ToString("yyyy-MM-dd HH:mm:ss"), guid, LogName);
                if (syncurl != "")
                {
                    try
                    {
                        T9.Util.HttpResponseUtil.SendRequest(syncurl + "?otype=ChangeToken&newSupToken=" + model.BasicSupToken + "&newSupTokenTime=" + model.BasicSupToken_Time.ToString("yyyy-MM-dd HH:mm:ss"), "");
                    }
                    catch (Exception ex)
                    {
                        string ogMsg = "\r\n报错信息:" + ex.Message.ToString();
                        ogMsg += "\r\n报错详情:" + ex.StackTrace.ToString();
                        T9.Util.LogUtil.WriteLog(ogMsg, guid, LogName);
                    }
                }
                return model.BasicSupToken;

2.进行accessToken超时校验

 #region 进行Token和Ticket超时校验
        /// <summary>
        /// 进行Token和Ticket超时校验
        /// </summary>
        /// <param name="BasicSupToken_Time"></param>
        /// <param name="BasicSupTicket_Time"></param>
        /// <returns>false:没有超时;true:超时</returns>
        public static bool checkTokenTicketTime(DateTime BasicSupToken_Time, DateTime BasicSupTicket_Time)
        {
            TimeSpan ts1 = new TimeSpan(BasicSupToken_Time.Ticks);
            TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
            TimeSpan ts3 = ts1.Subtract(ts2).Duration();
            if (ts3.TotalSeconds > 7000)
            {
                return true;
            }
            ts1 = new TimeSpan(BasicSupTicket_Time.Ticks);
            ts2 = new TimeSpan(DateTime.Now.Ticks);
            ts3 = ts1.Subtract(ts2).Duration();
            if (ts3.TotalSeconds > 7000)
            {
                return true;
            }
            return false;
        }
        #endregion

3.异常情况重置accesstoken

#region 非正常流程下 重置Token
        /// <summary>
        /// 非正常流程下 重置Token
        /// </summary>
        /// <param name="WXAPPID"></param>
        /// <param name="WXSecret"></param>
        /// <param name="syncurl"></param>
        /// <param name="returnMsg"></param>
        /// <param name="LogName"></param>
        /// <param name="guid"></param>
        /// <returns></returns>
        public static bool ResetBasicSupToken(string WXAPPID, string WXSecret, string syncurl, string returnMsg, string LogName = null, string guid = null)
        {
            if (returnMsg.Contains("40001") || returnMsg.Contains("40003") || returnMsg.Contains("40014") || returnMsg.Contains("41001") || returnMsg.Contains("42001") || returnMsg.Contains("42007") || returnMsg.Contains("9001003") || string.IsNullOrWhiteSpace(returnMsg))
            {
                T9.Util.LogUtil.WriteLog("token失效重新生成", guid, LogName == null ? "WebLog" : LogName);
                string token = getAccessToken(WXAPPID, WXSecret, syncurl, LogName, true, guid);
                string ticket = getJsapiTicket(WXAPPID, WXSecret, syncurl, token, LogName == null ? "WebLog" : LogName, true, guid);
                AccessTokenModel model = new AccessTokenModel(WXAPPID, token, ticket, DateTime.Now);
                CommonApi.AdddicAccessToken(model);
                return true;
            }
            return false;
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

可曾听闻丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值