Esempio n. 1
0
        /// <summary>
        /// 根据code获取Access_Token,返回内容中还包含用户的OpenID
        /// </summary>
        /// <param name="code">用户授权时获取的code</param>
        /// <returns></returns>
        public static AccessTokenResultModel GetAccessToken(string appid, string appsecret, string code)
        {
            string url = "https://api.weixin.qq.com/sns/oauth2/access_token";

            url += "?appid=" + appid;
            url += "&secret=" + appsecret;
            url += "&code=" + code;
            url += "&grant_type=authorization_code";


            var res = new HttpUtil( ).Get(url);

            Log.Logger.Log("[wx: GetAccessToken] " + url + "#" + res);

            try
            {
                AccessTokenResultModel wx_token = JsonUtil.FromJson <AccessTokenResultModel>(res);
                if (wx_token != null && wx_token.errcode == 0)
                {
                    return(wx_token);
                }
                else
                {
                    Log.Logger.Log("[wx: GetAccessToken Failed] " + url + "#" + res);
                    return(null);
                }
            }
            catch (Exception ex)
            {
                Log.Logger.Log("[wx: GetAccessToken Exception] " + url + "#" + ex.Message);
                return(null);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 获取AccessToken
        /// </summary>
        /// <returns></returns>
        public string GetAccessToken()
        {
            var manager = new WeChatPublicNumberManager(_weChatConfig);
            AccessTokenResultModel result = manager.GetAccessToken();

            ConsoleWriteProperties(result);
            return(result.AccessToken);
        }
Esempio n. 3
0
        public static bool IsAccessTokenValid()
        {
            AccessTokenResultModel accessToken = accessTokenTable.SelectRecord();

            DateTime creationTime = accessToken.DateCreated;
            DateTime currentTime  = DateTime.Now;

            double timeDifference = (currentTime - creationTime).TotalSeconds;
            double expireLength   = (double)accessToken.ExpiresIn;

            if (timeDifference <= expireLength)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// 获取AccessToken
        /// </summary>
        /// <returns>OpenID</returns>
        public AccessTokenResultModel GetAccessToken()
        {
            var data = new WeChatDataModel();

            data.SetValue("appid", Config.APPID);
            data.SetValue("secret", Config.APPSECRET);
            data.SetValue("grant_type", "client_credential");
            string   url          = $"{Config.WeChatAPIUrl}cgi-bin/token?{data.ToUrlParams()}";
            string   weChatResult = WeChatHttpManager.Get(url);
            JsonData jsonData     = JsonMapper.ToObject(weChatResult);

            if (WeChatPublicNumberErrorHelper.IsError(jsonData))
            {
                throw WeChatPublicNumberErrorHelper.GetWeChatException(jsonData);
            }
            var accessToken = new AccessTokenResultModel
            {
                AccessToken = jsonData["access_token"].ToString(),
                ExpiresIn   = Convert.ToInt32(jsonData["expires_in"].ToString())
            };

            return(accessToken);
        }
Esempio n. 5
0
        public static string GetAccessToken()
        {
            AccessTokenResultModel accessToken = accessTokenTable.SelectRecord();

            return(accessToken.AccessToken);
        }