Example #1
0
        /// <summary>
        /// 注册企业号
        /// </summary>
        /// <param name="CorpID"></param>
        /// <param name="Secret"></param>
        private static void Register(string CorpID, string Secret)
        {
            string         key = GetFinalKey(CorpID, Secret);
            AccessTokenBag bag = new AccessTokenBag();

            bag.CorpID                 = CorpID;
            bag.Secret                 = Secret;
            bag.ExpireTime             = DateTime.MinValue;
            bag.AccessTokenJsonResult  = new AccessTokenJsonResult();
            AccessTokenCollection[key] = bag;
        }
Example #2
0
        private static AccessTokenJsonResult GetAccessTokenResult(string key, bool getNewToken = false)
        {
            if (!AccessTokenCollection.ContainsKey(key))
            {
                throw new Exception(string.Format("企业号{0}尚未在代码里注册!", key));
            }
            AccessTokenBag accessTokenBag = AccessTokenCollection[key];

            //用户选择重新获取或者token已经过期
            if (getNewToken || DateTime.Now > accessTokenBag.ExpireTime)
            {
                accessTokenBag.AccessTokenJsonResult = ApiHelper.Get <AccessTokenJsonResult>(CommonApi.GetAccessToken(accessTokenBag.CorpID, accessTokenBag.Secret));
                //考虑到网络延时提前20秒过期
                accessTokenBag.ExpireTime = DateTime.Now.AddSeconds(accessTokenBag.AccessTokenJsonResult.expires_in - 20);
            }
            return(accessTokenBag.AccessTokenJsonResult);
        }