Esempio n. 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);
        }
Esempio n. 2
0
        /// <summary>
        /// Gets the OAuth token for the user associated with the provided code.
        /// </summary>
        /// <param name="code">Sent by reddit as a parameter in the return uri.</param>
        /// <param name="isRefresh">Set to true for refresh requests.</param>
        /// <returns></returns>
        public string GetOAuthToken(string code, bool isRefresh = false)
        {
            if (Type.GetType("Mono.Runtime") != null)
            {
                ServicePointManager.ServerCertificateValidationCallback = (s, c, ch, ssl) => true;
            }
            _webAgent.Cookies = new CookieContainer();

            var request = _webAgent.CreatePost(AccessUrl);

            request.InitWebReqProxy();
            request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(_clientId + ":" + _clientSecret));
            var stream = request.GetRequestStream();

            if (isRefresh)
            {
                _webAgent.WritePostBody(stream, new
                {
                    grant_type    = "refresh_token",
                    refresh_token = code
                });
            }
            else
            {
                _webAgent.WritePostBody(stream, new
                {
                    grant_type = "authorization_code",
                    code,
                    redirect_uri = _redirectUri
                });
            }

            stream.Close();
            var json = _webAgent.ExecuteRequest(request);

            if (json["access_token"] != null)
            {
                if (json["refresh_token"] != null)
                {
                    RefreshToken = json["refresh_token"].ToString();
                }
                OAuthToken = json["access_token"].ToString();
                return(json["access_token"].ToString());
            }
            throw new AuthenticationException("Could not log in.");
        }