Beispiel #1
0
        /// <summary>
        /// Logs in the current Reddit instance.
        /// </summary>
        /// <param name="username">The username of the user to log on to.</param>
        /// <param name="password">The password of the user to log on to.</param>
        /// <param name="useSsl">Whether to use SSL or not. (default: true)</param>
        /// <returns></returns>
        public AuthenticatedUser LogIn(string username, string password, bool useSsl = true)
        {
            if (Type.GetType("Mono.Runtime") != null)
            {
                ServicePointManager.ServerCertificateValidationCallback = (s, c, ch, ssl) => true;
            }
            _webAgent.Cookies = new CookieContainer();
            HttpWebRequest request;

            if (useSsl)
            {
                request = _webAgent.CreatePost(SslLoginUrl);
            }
            else
            {
                request = _webAgent.CreatePost(LoginUrl);
            }
            var stream = request.GetRequestStream();

            if (useSsl)
            {
                _webAgent.WritePostBody(stream, new
                {
                    user     = username,
                    passwd   = password,
                    api_type = "json"
                });
            }
            else
            {
                _webAgent.WritePostBody(stream, new
                {
                    user     = username,
                    passwd   = password,
                    api_type = "json",
                    op       = "login"
                });
            }
            stream.Close();
            var response = (HttpWebResponse)request.GetResponse();
            var result   = _webAgent.GetResponseString(response.GetResponseStream());
            var json     = JObject.Parse(result)["json"];

            if (json["errors"].Count() != 0)
            {
                throw new AuthenticationException("Incorrect login.");
            }

            InitOrUpdateUser();

            return(User);
        }
        public AuthenticatedUser GetUser(string accessToken)
        {
            var request = _webAgent.CreateGet(OauthGetMeUrl);

            request.Headers["Authorization"] = String.Format("bearer {0}", accessToken);
            var response  = (HttpWebResponse)request.GetResponse();
            var result    = _webAgent.GetResponseString(response.GetResponseStream());
            var thingjson = "{\"kind\": \"t2\", \"data\": " + result + "}";
            var json      = JObject.Parse(thingjson);

            return(new AuthenticatedUser().Init(new Reddit(), json, _webAgent));
        }