public OWebAccessToken GetWebAccessToken()
        {
            //获取网页访问令牌及OpenID
            string url = _api + "?appid=" + _appId;

            url += "&secret=" + _appSecret;
            url += "&code=" + _code;
            url += "&grant_type=authorization_code";

            //if (_accessToken.ErrorCode == 0 && !string.IsNullOrEmpty(_accessToken.AccessToken)) {
            //    //刷新令牌
            //}

            Logger.Debug("OWebAccessTokenAPI URL:" + url);

            //重新获取令牌
            try {
                using (WebClient wc = new WebClient()) {
                    string result = wc.DownloadString(url);

                    Logger.Debug("OWebAccessTokenAPI.GetWebAccessToken:" + result);

                    var token = JsonKit.Deserialize <access_token_obj>(result);
                    if (token != null)
                    {
                        if (!string.IsNullOrEmpty(token.access_token))
                        {
                            OWebAccessToken at = new OWebAccessToken();
                            at.ErrorCode       = token.errcode;
                            at.ErrorMessage    = token.errmsg;
                            at.AccessToken     = token.access_token;
                            at.ExpiresInterval = token.expires_in;
                            at.RefreshToken    = token.refresh_token;
                            at.OpenId          = token.openid;
                            at.Scope           = token.scope;

                            return(at);
                        }
                        else
                        {
                            return(new OWebAccessToken()
                            {
                                ErrorCode = token.errcode, ErrorMessage = token.errmsg
                            });
                        }
                    }
                }
            } catch (Exception ex) {
                Logger.Error("OWebAccessTokenAPI Error", ex);

                return(new OWebAccessToken()
                {
                    ErrorCode = -1, ErrorMessage = ex.Message
                });
            }
            return(new OWebAccessToken()
            {
                ErrorCode = -1, ErrorMessage = "接口没有获取到正确结果!"
            });
        }
示例#2
0
        /// <summary>
        /// 根据微信 Code 获取微信 OpenID
        /// </summary>
        /// <returns></returns>
        public string GetOpenId(string appId, string appSecret, string code)
        {
//#if !DEBUG
            //获取缓存
            string openId = CookiesKit.GetCookie("OpenId");

            if (!string.IsNullOrEmpty(openId))
            {
                return(openId);
            }

            if (!string.IsNullOrEmpty(code))
            {
                OWebAccessTokenAPI atapi = new OWebAccessTokenAPI(appId, appSecret, code);
                OWebAccessToken    at    = atapi.GetWebAccessToken();
                CookiesKit.SetCookie("OpenId", at.OpenId);

                return(at.OpenId);
            }
            return(string.Empty);
//#else
//            return "ornCZt1qWRaya7C3Xd1-pN1ktFrI";
//#endif
        }