Exemple #1
0
        public async Task <string> GetAccessToken()
        {
            string key = "WeChatAccessToken";
            var    accessTokenResultStr = await this.GetFromCacheServer(key);

            WeChatAccessToken accessTokenResult = null;
            Action <JObject>  callback          = jObject =>
            {
                var expiresAt = DateTime.Now.AddHours(1.5);
                accessTokenResult = new WeChatAccessToken
                {
                    AccessToken = jObject.GetValue("access_token").Value <string>(),
                    ExpiresIn   = jObject.GetValue("expires_in").Value <int>(),
                    ExpiresAt   = expiresAt
                };
                this.PostDataToCacheServer(key, accessTokenResult);
            };

            if (!string.IsNullOrEmpty(accessTokenResultStr))
            {
                accessTokenResult = JsonConvert.DeserializeObject <WeChatAccessToken>(accessTokenResultStr);
            }
            else
            {
                await this.GetFromWechatServer(_weChatApiUrls.GetAcessTokenUrl(), callback);
            }
            return(accessTokenResult?.AccessToken);
        }
Exemple #2
0
        private Task PostDataToCacheServer(string key, WeChatAccessToken value)
        {
            var cacheObj = new {
                Key     = key,
                Value   = JsonConvert.SerializeObject(value),
                Options = new
                {
                    AbsoluteExpirationRelativeToNow = value.ExpiresAt.TimeOfDay
                }
            };

            return(client.PostAsJsonAsync(_url, cacheObj));
        }