예제 #1
0
        /// <summary>
        /// 获取access_token。会缓存,过期后自动重新获取新的token。
        /// </summary>
        public static TokenResult GetAccessToken()
        {
            var appId  = WeChatConfig.GetAppId();
            var secret = WeChatConfig.GetSecret();
            var result = CacheHelper.Get("AccessToken" + appId) as TokenResult;

            if (result == null || string.IsNullOrWhiteSpace(result.AccessToken))
            {
                result = HttpHelper.Get <TokenResult>(ApiList.GetTokenUrl, new
                {
                    grant_type = "client_credential",
                    appid      = appId,
                    secret     = secret
                });
                if (result.IsSuccess)
                {
                    CacheHelper.Set("AccessToken" + appId, result, result.ExpiresIn - 60);
                }
                else
                {
                    LogHelper.Debug("GetAccessToken失败!" + result.ErrMsg, "微信_Fail_");
                }
            }
            return(result);
        }
예제 #2
0
        /// <summary>
        /// 传入Code
        /// </summary>
        public static Dictionary <string, string> GetOauth2AccessToken(WeChatParam param)
        {
            var param2 = new
            {
                appid      = WeChatConfig.GetAppId(),
                secret     = WeChatConfig.GetSecret(),
                code       = param.Code,
                grant_type = "authorization_code"
            };
            var dic = HttpHelper.Get <Dictionary <string, string> >(ApiList.GetOauth2AccessTokenUrl, param2);

            return(dic);
        }