Ejemplo n.º 1
0
        /// <summary>
        /// 获取票据信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public override async Task <AccessTokenObject> GetAccessToken(string code)
        {
            using (HttpClient httpClient = new HttpClient())
            {
                //2、通过Authorization Code获取Access Token
                StringBuilder tokenUrl = new StringBuilder();
                tokenUrl.Append("client_id=" + ClientId);          //申请应用时分配的AppKey
                tokenUrl.Append("&client_secret=" + ClientSecret); //申请应用时分配的AppSecret
                tokenUrl.Append("&grant_type=authorization_code"); //请求的类型,填写authorization_code
                tokenUrl.Append("&code=" + code);                  //调用authorize获得的code值
                tokenUrl.Append("&redirect_uri=" + CallbackUrl);   //回调地址,需需与注册应用里的回调地址一致
                StringContent fromurlcontent = new StringContent(tokenUrl.ToString());
                fromurlcontent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
                var token_url        = "https://api.weibo.com/oauth2/access_token";
                var responseTokenMsg = await httpClient.PostAsync(new Uri(token_url), fromurlcontent);

                var responseToken = await responseTokenMsg.Content.ReadAsStringAsync();

                var dynamicObject = JsonConvert.DeserializeObject <dynamic>(responseToken);
                var access_token  = dynamicObject.access_token.ToString();//票据
                var uid           = dynamicObject.uid.ToString();

                var accessTokenObject = new AccessTokenObject()
                {
                    UserId      = uid,
                    AccessToken = access_token
                };
                return(accessTokenObject);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public override async Task <OAuthUserInfo> GetUserInfo(AccessTokenObject accessTokenObject)
        {
            using (HttpClient httpClient = new HttpClient())
            {
                //4、OpenAPI调用,获取用户信息
                StringBuilder getUserInfoUrl = new StringBuilder();
                getUserInfoUrl.Append("https://graph.qq.com/user/get_user_info");
                getUserInfoUrl.Append("?oauth_consumer_key=" + ClientId);
                getUserInfoUrl.Append("&access_token=" + accessTokenObject.AccessToken);
                getUserInfoUrl.Append("&openid=" + accessTokenObject.UserId);
                getUserInfoUrl.Append("&format=json");

                var str = await httpClient.GetStringAsync(getUserInfoUrl.ToString());

                var dynamicObject = JsonConvert.DeserializeObject <dynamic>(str);
                var user          = new OAuthUserInfo()
                {
                    Id     = accessTokenObject.UserId,
                    Name   = dynamicObject.nickname.ToString(),
                    ImgUrl = dynamicObject.figureurl_qq_2.ToString(),
                    Gender = dynamicObject.gender.ToString() == "男" ? true : false
                };
                return(user);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 获取票据信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public override async Task <AccessTokenObject> GetAccessToken(string code)
        {
            using (HttpClient httpClient = new HttpClient())
            {
                //2、通过Authorization Code获取Access Token
                StringBuilder tokenUrl = new StringBuilder();
                tokenUrl.Append("https://graph.qq.com/oauth2.0/token");
                tokenUrl.Append("?grant_type=authorization_code"); //授权类型,在本步骤中,此值为“authorization_code”。
                tokenUrl.Append("&client_id=" + ClientId);         //申请QQ登录成功后,分配给网站的appid。
                tokenUrl.Append("&client_secret=" + ClientSecret); //申请QQ登录成功后,分配给网站的appkey。
                #region 注释

                /*
                 *  上一步返回的authorization code。
                 *  如果用户成功登录并授权,则会跳转到指定的回调地址,并在URL中带上Authorization Code。
                 *  例如,回调地址为www.qq.com/my.php,则跳转到:
                 *  http://www.qq.com/my.php?code=520DD95263C1CFEA087******
                 *  注意此code会在10分钟内过期。
                 */
                #endregion
                tokenUrl.Append("&code=" + code);
                tokenUrl.Append("&redirect_uri=" + CallbackUrl);//与上面一步中传入的redirect_uri保持一致。
                var responseToken = await httpClient.GetStringAsync(tokenUrl.ToString());

                //3、获取用户OpenID
                var access_token         = responseToken.Split('&')[0].Split('=')[1];//票据
                var access_token_url     = "https://graph.qq.com/oauth2.0/me?access_token=" + access_token;
                var responseAccess_token = await httpClient.GetStringAsync(access_token_url);

                Regex  reg           = new Regex("^callback[(](?<data>.*?)[);]+$", RegexOptions.IgnoreCase);
                Match  match         = reg.Match(responseAccess_token.Trim());
                string jsonText      = match.Groups["data"].Value;
                var    dynamicObject = JsonConvert.DeserializeObject <dynamic>(jsonText);
                //openid是此网站上唯一对应用户身份的标识,
                //网站可将此ID进行存储便于用户下次登录时辨识其身份,或将其与用户在网站上的原有账号进行绑定。
                var openid = dynamicObject.openid.ToString();

                var accessTokenObject = new AccessTokenObject()
                {
                    AccessToken = access_token,
                    UserId      = openid
                };
                return(accessTokenObject);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="accessTokenObject"></param>
        /// <returns></returns>
        public override async Task <OAuthUserInfo> GetUserInfo(AccessTokenObject accessTokenObject)
        {
            using (HttpClient httpClient = new HttpClient())
            {
                //3、获取用户信息
                var userInfo = await httpClient.GetStringAsync("https://api.weibo.com/2/users/show.json?access_token=" + accessTokenObject.AccessToken + "&uid=" + accessTokenObject.UserId);

                //授权用户的UID,本字段只是为了方便开发者,减少一次user/show接口调用而返回的
                //,第三方应用不能用此字段作为用户登录状态的识别,只有access_token才是用户授权的唯一票据。
                var dynamicObject = JsonConvert.DeserializeObject <dynamic>(userInfo);
                var user          = new OAuthUserInfo()
                {
                    Id     = accessTokenObject.UserId,
                    Name   = dynamicObject.name.ToString(),
                    Gender = dynamicObject.gender.ToString() == "m" ? true : false,
                    ImgUrl = dynamicObject.avatar_hd.ToString()
                };
                return(user);
            }
        }
Ejemplo n.º 5
0
 public virtual Task <OAuthUserInfo> GetUserInfo(AccessTokenObject code)
 {
     throw new NotImplementedException();
 }