GetProviderToken() public static method

获取应用提供商凭证
public static GetProviderToken ( string corpId, string providerSecret, int timeOut = Config.TIME_OUT ) : ProviderTokenResult
corpId string
providerSecret string
timeOut int
return Senparc.Weixin.QY.Entities.ProviderTokenResult
Exemplo n.º 1
0
        /// <summary>
        /// 获取可用Token
        /// </summary>
        /// <param name="corpId"></param>
        /// <param name="getNewToken">是否强制重新获取新的Token</param>
        /// <returns></returns>
        public static ProviderTokenResult GetTokenResult(string corpId, bool getNewToken = false)
        {
            if (!ProviderTokenCollection.ContainsKey(corpId))
            {
                throw new WeixinException("此CorpId尚未注册,请先使用ProviderTokenContainer.Register完成注册(全局执行一次即可)!");
            }

            var accessTokenBag = ProviderTokenCollection[corpId];

            lock (accessTokenBag.Lock)
            {
                if (getNewToken || accessTokenBag.ExpireTime <= DateTime.Now)
                {
                    //已过期,重新获取
                    accessTokenBag.ProviderTokenResult = CommonApi.GetProviderToken(accessTokenBag.CorpId,
                                                                                    accessTokenBag.CorpSecret);
                    accessTokenBag.ExpireTime = DateTime.Now.AddSeconds(7200);
                }
            }
            return(accessTokenBag.ProviderTokenResult);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 获取可用Token
        /// </summary>
        /// <param name="corpId"></param>
        /// <param name="getNewToken">是否强制重新获取新的Token</param>
        /// <returns></returns>
        public static ProviderTokenResult GetTokenResult(string corpId, bool getNewToken = false)
        {
            if (!CheckRegistered(corpId))
            {
                throw new WeixinQyException(UN_REGISTER_ALERT);
            }

            var providerTokenBag = (ProviderTokenBag)ItemCollection[corpId];

            lock (providerTokenBag.Lock)
            {
                if (getNewToken || providerTokenBag.ExpireTime <= DateTime.Now)
                {
                    //已过期,重新获取
                    providerTokenBag.ProviderTokenResult = CommonApi.GetProviderToken(providerTokenBag.CorpId,
                                                                                      providerTokenBag.CorpSecret);
                    providerTokenBag.ExpireTime = DateTime.Now.AddSeconds(providerTokenBag.ProviderTokenResult.expires_in);
                }
            }
            return(providerTokenBag.ProviderTokenResult);
        }