예제 #1
0
        public String GetToken()
        {
            accessToken = "";// memcached.Get(AccessTokenCacheKey) as string;
            if (string.IsNullOrEmpty(accessToken))
            {
                string requestUrl = WeiXinPayConfig.TokenUrl + "?grant_type=client_credential&appid=" + config.AppId + "&secret=" + config.AppSecret;

                string result = HttpRequestHelper.DoGet(requestUrl, "", WeiXinPayConfig.Charset);
                if (!string.IsNullOrEmpty(result))
                {
                    JObject obj = JObject.Parse(result);
                    JToken  token;
                    if (obj.TryGetValue("access_token", out token))
                    {
                        accessToken = token.ToString().Replace("\"", "");
                        //memcached.Set(AccessTokenCacheKey, accessToken, DateTime.Now.AddMinutes(100));
                    }
                }
            }
            else
            {
                accessToken = accessToken.Replace("\"", "");
            }
            return(accessToken);
        }
예제 #2
0
        /// <summary>
        /// code 换取 session_key,openid
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="secret"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public SessionkeyRsp GetSessionkeyOpenIdByCode(string code)
        {
            var url = WeiXinPayConfig.SessionkeyAndOpenidUrl
                      .Replace("{appId}", config.AppId).Replace("{secret}", config.AppSecret).Replace("{code}", code);
            var result = HttpRequestHelper.DoGet(url, "");
            var rsp    = JsonConvert.DeserializeObject <SessionkeyRsp>(result);

            return(rsp);
        }