public static GetComponentAccessToken ( string componentAppId, string componentAppSecret, string componentVerifyTicket, int timeOut = Config.TIME_OUT ) : |
||
componentAppId | string | 第三方平台appid |
componentAppSecret | string | 第三方平台appsecret |
componentVerifyTicket | string | 微信后台推送的ticket,此ticket会定时推送,具体请见本页末尾的推送说明 |
timeOut | int | |
return |
/// <summary> /// 获取可用Token /// </summary> /// <param name="componentAppId"></param> /// <param name="componentVerifyTicket"></param> /// <param name="getNewToken">是否强制重新获取新的Token</param> /// <returns></returns> public static ComponentAccessTokenResult GetTokenResult(string componentAppId, string componentVerifyTicket, bool getNewToken = false) { if (!ComponentAccessTokenCollection.ContainsKey(componentAppId)) { throw new WeixinException("此appId尚未注册,请先使用ComponentAccessTokenContainer.Register完成注册(全局执行一次即可)!"); } var accessTokenBag = ComponentAccessTokenCollection[componentAppId]; lock (accessTokenBag.Lock) { if (getNewToken || accessTokenBag.ExpireTime <= DateTime.Now) { //已过期,重新获取 accessTokenBag.ComponentAccessTokenResult = CommonApi.GetComponentAccessToken(accessTokenBag.ComponentAppId, accessTokenBag.ComponentAppSecret, componentVerifyTicket); accessTokenBag.ExpireTime = DateTime.Now.AddSeconds(accessTokenBag.ComponentAccessTokenResult.expires_in); } } return(accessTokenBag.ComponentAccessTokenResult); }
/// <summary> /// 获取可用AccessToken /// </summary> /// <param name="componentAppId"></param> /// <param name="componentVerifyTicket"></param> /// <param name="getNewToken">是否强制重新获取新的Token</param> /// <returns></returns> public static ComponentAccessTokenResult GetComponentAccessTokenResult(string componentAppId, string componentVerifyTicket, bool getNewToken = false) { if (!CheckRegistered(componentAppId)) { throw new WeixinOpenException(UN_REGISTER_ALERT); } var accessTokenBag = ItemCollection[componentAppId]; lock (accessTokenBag.Lock) { if (getNewToken || accessTokenBag.ComponentAccessTokenExpireTime <= DateTime.Now) { //已过期,重新获取 //var componentVerifyTicket = TryGetComponentVerifyTicket(componentAppId); accessTokenBag.ComponentAccessTokenResult = CommonApi.GetComponentAccessToken(accessTokenBag.ComponentAppId, accessTokenBag.ComponentAppSecret, componentVerifyTicket); accessTokenBag.ComponentAccessTokenExpireTime = DateTime.Now.AddSeconds(accessTokenBag.ComponentAccessTokenResult.expires_in); } } return(accessTokenBag.ComponentAccessTokenResult); }