Ejemplo n.º 1
0
        public RedditAccessToken Authenticate(string redditUsername, string redditPassword, string clientId, string clientSecret)
        {
            username = redditUsername;

            var authenticationArray         = Encoding.ASCII.GetBytes($"{clientId}:{clientSecret}");
            var encodedAuthenticationString = Convert.ToBase64String(authenticationArray);

            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", encodedAuthenticationString);

            client.DefaultRequestHeaders.Add("User-Agent", $"{_clientName} by {redditUsername}");

            var formData = new Dictionary <string, string> {
                { "grant_type", "password" },
                { "username", redditUsername },
                { "password", redditPassword }
            };

            var encodedFormData = new FormUrlEncodedContent(formData);
            var authUrl         = "https://www.reddit.com/api/v1/access_token";
            var response        = client.PostAsync(authUrl, encodedFormData).GetAwaiter().GetResult();

            // Response Code
            Console.WriteLine(response.StatusCode);

            // Actual Token
            var responseData = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
            var accessToken  = JObject.Parse(responseData).SelectToken("access_token").ToString();

            // Update AuthorizationHeader
            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("bearer", accessToken);

            responseData = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();

            Console.WriteLine(responseData);

            RedditAccessToken token = new RedditAccessToken(responseData);

            Console.WriteLine("-- Authentication OK! --");
            Console.WriteLine("\n\n");

            return(token);
        }
Ejemplo n.º 2
0
        // Collect the access_token and set the default request headers
        private void Authorization(Dictionary <string, string> content)
        {
            var encodedContent = new FormUrlEncodedContent(content);

            // Api-url for login
            var authUrl = "https://www.reddit.com/api/v1/access_token";

            // Post the form data and save the HttpResponeMessage
            _response = _client.PostAsync(authUrl, encodedContent).GetAwaiter().GetResult();

            // Response Code
            Console.WriteLine(_response.StatusCode);

            // Actual Token

            _responseData = _response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
            var accessToken = JObject.Parse(_responseData).SelectToken("access_token").ToString();

            // Sets the DefaultRequestHeaders
            _client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("bearer", accessToken);
            RAtoken = new RedditAccessToken(accessToken, JObject.Parse(_responseData).SelectToken("token_type").ToString(), Convert.ToInt16(JObject.Parse(_responseData).SelectToken("expires_in")));
        }