zoukankan      html  css  js  c++  java
  • 微信消息推送,获取access_token时AppSecret错误或者access_token无效 invalid credential, access_token is invalid or not latest rid

    最近微信推送消息出现:获取access_token时AppSecret错误或者access_token无效 invalid credential, access_token is invalid or not latest rid,    这个access_token 无效的问题,之前消息推送都是没有问题的,

    就最近一周定时器发送消息推送出现偶尔发送成功,偶尔发送提示这个access_token 的问题

    前提:微信公众号 AppId 和AppSecret 都没有错的情况下,之前都可以

    造成这个问题的原因是:微信获取access_token 接口调用量/上线次数 达到了顶峰 10000,如图1所示:

    如图1 所示,调用接口次数达到上线,导致获取access_token 失败的问题;

    于是查找 code ,发现access_token 获取居然是没进行全局缓存记录下来,直接每次调用接口获取一次,如下代码所示:

     /// <summary>
            ///  获取会员微信息
            /// </summary>
            /// <param name="openid"></param>
            /// <returns></returns>
            public UserInfoJson GetUserInfo(string openid)
            {
                AccessTokenContainer.Register(WeiXinConfig.AppId, WeiXinConfig.AppSecret);
                var token = AccessTokenContainer.GetAccessToken(WeiXinConfig.AppId);
                UserInfoJson result = null;
                try
                {
                    result = UserApi.Info(token, openid);
                    this._logHandle.LogInfo("GetUserInfo 获取会员微信信息:"+result.nickname);
                }
                catch (Exception ex)
                {
                    this._logHandle.LogException(ex, "GetUserInfo 获取会员微信信息失败! openid: " + openid);
                    this._logHandle.LogInfo("GetUserInfo 获取会员微信信息失败:" +ex.Message);
                }
                return result;
            }

    果然是个坑,于是将上述代码做修改,并且在全局进行微信相关注册缓存操作

     /// <summary>
            ///  获取会员微信息
            /// </summary>
            /// <param name="openid"></param>
            /// <returns></returns>
            public UserInfoJson GetUserInfo(string openid)
            {
                //AccessTokenContainer.Register(WeiXinConfig.AppId, WeiXinConfig.AppSecret);
                //var token = AccessTokenContainer.GetAccessToken(WeiXinConfig.AppId);
                UserInfoJson result = null;
                try
                {
                    result = UserApi.Info(WeiXinConfig.AppId, openid);
                    this._logHandle.LogInfo("GetUserInfo 获取会员微信信息:"+result.nickname);
                }
                catch (Exception ex)
                {
                    this._logHandle.LogException(ex, "GetUserInfo 获取会员微信信息失败! openid: " + openid);
                    this._logHandle.LogInfo("GetUserInfo 获取会员微信信息失败:" +ex.Message);
                }
                return result;
            }

    global.asax.cs

     protected void Application_Start()
            {
                // winxin
                var isGLobalDebug = true;//设置全局 Debug 状态
                var senparcSetting = SenparcSetting.BuildFromWebConfig(isGLobalDebug);
                var register = RegisterService.Start(senparcSetting).UseSenparcGlobal();//CO2NET全局注册,必须!
                //var register = RegisterService.Start(senparcSetting).UseSenparcGlobal();//必须 
                var isWeixinDebug = true;//设置微信 Debug 状态
                var senparcWeixinSetting = SenparcWeixinSetting.BuildFromWebConfig(isWeixinDebug);
                register.UseSenparcWeixin(senparcWeixinSetting, senparcSetting);////微信全局注册,必须!
    
    
    
             
            }

    Startup.cs

     public void Configuration(IAppBuilder app)
            {
                // 有关如何配置应用程序的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkID=316888
                ConfigureAuth(app);
                ConfigureJob(app);
    
                MemberService.RegisterSenparc(); 
            }

     MemberService.cs

     //注册微信 
            public  static void RegisterSenparc()
            {
                
                AccessTokenContainer.Register(WeiXinConfig.AppId, WeiXinConfig.AppSecret);
            }

    在此记录下,避免以后出现相关的坑

  • 相关阅读:
    CodeForces 347B Fixed Points (水题)
    CodeForces 347A Difference Row (水题)
    CodeForces 346A Alice and Bob (数学最大公约数)
    CodeForces 474C Captain Marmot (数学,旋转,暴力)
    CodeForces 474B Worms (水题,二分)
    CodeForces 474A Keyboard (水题)
    压力测试学习(一)
    算法学习(一)五个常用算法概念了解
    C#语言规范
    异常System.Threading.Thread.AbortInternal
  • 原文地址:https://www.cnblogs.com/zty-Love/p/13563235.html
Copyright © 2011-2022 走看看