Ejemplo n.º 1
0
        public override string GetOpenId()
        {
            _accessToken = GetAccessToken();

            var url = $"{OpenID_Url}?access_token={_accessToken.access_token}";

            // 如果要获取unionid,先去申请https://wiki.connect.qq.com/unionid%E4%BB%8B%E7%BB%8D
            if (param.withUnionid == true)
            {
                url += "&unionid=1";
            }

            var result = HttpUtil.Get(url);

            //callback( {"client_id":"YOUR_APPID","openid":"YOUR_OPENID"} );
            //callback( {"client_id":"YOUR_APPID","openid":"YOUR_OPENID","unionid":"YOUR_UNIONID"} );
            result = result.Replace("callback(", "").Replace(")", "").Trim(';');

            var openid = JsonConvert.DeserializeObject <qqOpenIDEntity>(result);

            if (openid == null)
            {
                throw new Exception("获取qq OPENID 出错");
            }
            _unionid = openid.unionid;
            return(openid.openid);
        }
Ejemplo n.º 2
0
        private qqAccessTokenEntity GetAccessToken()
        {
            var code   = System.Web.HttpContext.Current.Request.QueryString["code"];
            var url    = $"{AccessToken_Url}?client_id={param.client_id}&client_secret={param.client_secret}&code={code}&redirect_uri={param.redirect_uri}&grant_type=authorization_code";
            var result = HttpUtil.Get(url);

            var arr       = result.Split('&');
            var resultDic = new Dictionary <string, string>();

            foreach (var ar in arr)
            {
                if (ar.Contains("="))
                {
                    resultDic.Add(ar.Split('=')[0], ar.Split('=')[1]);
                }
            }
            qqAccessTokenEntity qq_AccessToken = new qqAccessTokenEntity
            {
                access_token = resultDic.ContainsKey("access_token") ? resultDic["access_token"] : "",
                expires_in   = resultDic.ContainsKey("expires_in") ? resultDic["expires_in"] : "",
            };

            if (qq_AccessToken == null)
            {
                throw new Exception("获取qq ACCESS_TOKEN 出错");
            }
            return(qq_AccessToken);
        }