Beispiel #1
0
        /// <summary>
        /// 刷新令牌
        /// </summary>
        /// <param name="refreshToken"></param>
        /// <returns></returns>
        public override Entities.Avatar RefreshToken(string refreshToken)
        {
            var now    = DateTime.Now;
            var avatar = Avatar ?? new Entities.Avatar();
            var url    = "https://login.microsoftonline.com/common/oauth2/v2.0/token";

            Logger.Info(Channel.SerializeXml());

            var data =
                $"grant_type=refresh_token&refresh_token={refreshToken}&redirect_uri={HttpUtility.UrlEncode(Channel.CodeCallback)}&client_id={Channel.AppId}&client_secret={Channel.AppSecret}&scope={Channel.Parameters}";

            Logger.Info("data=" + data);
            try
            {
                var r        = new BrowserClient();
                var response = r.SendHttpRequest(url, true, "POST", data, null, null, null, "utf-8", "application/json",
                                                 "application/x-www-form-urlencoded");
                Logger.Info(response);
                var token = response.Content.JsonToObjContract <Response.MsonlineToken>();
                if (token != null)
                {
                    avatar.ChannelId      = Channel.Id;
                    avatar.Token          = token.AccessToken;
                    avatar.RefreshToken   = token.RefreshToken;
                    avatar.TokenGenerated = now;
                    avatar.TokenExpires   = now.AddSeconds(token.ExpiresIn);
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }

            return(avatar);
        }
Beispiel #2
0
        /// <summary>
        /// GET读取REST
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string ReadApi(string url)
        {
            Logger.Debug("READ API...");
            try
            {
                var kv = new Dictionary <string, object>
                {
                    { "Authorization", $"Bearer {Avatar.Token}" },
                    { "www-authenticate", $"Bearer {Avatar.Token}" }
                };

                var r        = new BrowserClient();
                var response = r.SendHttpRequest(url, true, "GET", "", kv, null, null, "utf-8", "application/json", "application/x-www-form-urlencoded");

                Logger.Info(response);
                return(response.Content);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
            return("");
        }
Beispiel #3
0
        /// <summary>
        /// oauth用code获取token
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public override Entities.Avatar ExchangeToken(string code)
        {
            var now    = DateTime.Now;
            var avatar = Avatar ?? new Entities.Avatar();
            var url    = "https://www.googleapis.com/oauth2/v4/token";

            var data =
                string.Format(
                    "grant_type=authorization_code&code={0}&redirect_uri={1}&client_id={2}&client_secret={3}",
                    code,
                    System.Web.HttpContext.Current.Server.UrlEncode(Channel.CodeCallback),
                    Channel.AppId,
                    Channel.AppSecret);

            try
            {
                var r        = new BrowserClient();
                var response = r.SendHttpRequest(url, true, "POST", data, null, null, null, "utf-8", "application/json", "application/x-www-form-urlencoded");
                Logger.Info(response);
                var token = response.Content.JsonToObjContract <Response.GoogleToken>();
                if (token != null)
                {
                    avatar.ChannelId      = Channel.Id;
                    avatar.Token          = token.AccessToken;
                    avatar.RefreshToken   = token.RefreshToken;
                    avatar.TokenGenerated = now;
                    avatar.TokenExpires   = now.AddSeconds(token.ExpiresIn);
                    avatar.SynchroTime    = now;
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }

            return(avatar);
        }
Beispiel #4
0
        /// <summary>
        /// 读取用户身份信息
        /// </summary>
        /// <returns></returns>
        public override Entities.Avatar ReadAvatar()
        {
            var now    = DateTime.Now;
            var avatar = Avatar ?? new Entities.Avatar();
            var url    = "https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token={0}";

            try
            {
                var r        = new BrowserClient();
                var response = r.SendHttpRequest(string.Format(url, avatar.Token), true, "GET", "", null, null, null,
                                                 "utf-8", "application/json");
                Logger.Info(response);
                var user = response.Content.JsonToObjContract <Response.GoogleUser>();
                if (user != null)
                {
                    avatar.ChannelIdentity = user.Id;
                    avatar.ChannelId       = Channel.Id;
                    avatar.DisplayName     = user.Name;
                    avatar.Code            = user.Id;
                }

                var email = ReadApi("https://www.googleapis.com/userinfo/email");
                if (email.Contains("email=") && email.Contains("&"))
                {
                    email       = email.Substring(email.IndexOf("email=", StringComparison.Ordinal) + 6);
                    email       = email.Substring(0, email.IndexOf("&", StringComparison.Ordinal));
                    avatar.Code = email;
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }

            return(avatar);
        }