Exemple #1
0
        public static async Task <IOAuth2Token> GetToken()
        {
            OAuth2Endpoint auth  = new OAuth2Endpoint(new ImgurClient(Properties.Settings.Default.ClientId, Properties.Settings.Default.ClientSecret));
            IOAuth2Token   token = null;

            if (!string.IsNullOrWhiteSpace(Properties.Settings.Default.Token))
            {
                token = JsonConvert.DeserializeObject <OAuth2Token>(Properties.Settings.Default.Token);
                if (token != null)
                {
                    if (token.ExpiresIn > 0)
                    {
                        return(token);
                    }
                    token = await auth.GetTokenByRefreshTokenAsync(token.RefreshToken);

                    Properties.Settings.Default.Token = JsonConvert.SerializeObject(token);
                    Properties.Settings.Default.Save();
                }
            }
            Process.Start(auth.GetAuthorizationUrl(Imgur.API.Enums.OAuth2ResponseType.Pin));
            string pin = await DialogCoordinator.Instance.ShowInputAsync(Context, "Enter Imgur Pin", "");

            token = await auth.GetTokenByPinAsync(pin);

            Properties.Settings.Default.Token = JsonConvert.SerializeObject(token);
            Properties.Settings.Default.Save();
            return(token);
        }
        private static async Task InitiateClient(bool getPin = true)
        {
            if (disable)
            {
                return;
            }
            var data = File.OpenText(Program.DataPath("imgflip pins", "txt")).ReadToEnd().Split('|');

            client = new ImgurClient($"{data[0]}", $"{data[1]}");
            end    = new OAuth2Endpoint(client);
            string g = end.GetAuthorizationUrl(Imgur.API.Enums.OAuth2ResponseType.Token);

            //System.Net.WebClient wc = new System.Net.WebClient();
            //byte[] raw = wc.DownloadData(g);

            //string webData = System.Text.Encoding.UTF8.GetString(raw);
            //Console.WriteLine(g);
            //ExtensionMethods.WriteToLog(ExtensionMethods.LogType.CustomMessage, null, g + "\n\n\n" + webData);
            if (getPin)
            {
                token = await end.GetTokenByPinAsync($"{data[2]}");

                client.SetOAuth2Token(token);
            }
            endpoint = new ImageEndpoint(client);
            disable  = true;
        }
Exemple #3
0
        public static async Task <IOAuth2Token> RequestImgurTokenFromPin(String code)
        {
            var client   = new ImgurClient("3f8dfa6f3541120", "dcb972ae2f6952efb8e9a156fe0c65b8136b3010");
            var endpoint = new OAuth2Endpoint(client);
            var token    = await endpoint.GetTokenByPinAsync(code);

            return(token);
        }
Exemple #4
0
        public async Task GetTokenByPinAsync_WithPinNull_ThrowsArgumentNullException()
        {
            var client   = new ImgurClient("123", "1234");
            var endpoint = new OAuth2Endpoint(client);

            var exception =
                await
                Record.ExceptionAsync(
                    async() => await endpoint.GetTokenByPinAsync(null).ConfigureAwait(false))
                .ConfigureAwait(false);

            Assert.NotNull(exception);
            Assert.IsType <ArgumentNullException>(exception);
        }
Exemple #5
0
        public async Task <bool> Login(string pin)
        {
            try {
                IOAuth2Token token = await _endpoint.GetTokenByPinAsync(pin);

                _client.SetOAuth2Token(token);

                FileIO.WriteRefreshToken(token.RefreshToken);

                User = token.AccountUsername;
                success.Show("Successfully logged in! Hi, " + User + "!", TimeSpan.FromSeconds(2));
                return(true);
            } catch (Exception ex) {
                error.Show("Wrong PIN? Could not login to Imgur! (" + ex.Message + ")", TimeSpan.FromSeconds(2));
                return(false);
            }
        }
Exemple #6
0
        public bool ValidateToken(string code)
        {
            var endpoint = new OAuth2Endpoint(Imgur);
            var token    = endpoint.GetTokenByPinAsync(code);

            token.Wait();
            accessToken = token.Result;
            Managers.Instance.user.UserName  = accessToken.AccountUsername;
            Managers.Instance.user.Token     = accessToken.AccessToken;
            Managers.Instance.user.Connected = true;
            MessageBox.Show("Successfully authenticated as " + accessToken.AccountUsername);
            var token_ = new OAuth2Token(accessToken.AccessToken, accessToken.RefreshToken, accessToken.TokenType,
                                         accessToken.AccountId, accessToken.AccountUsername, accessToken.ExpiresIn);

            Imgur = new ImgurClient(id, secretId, token_);
            return(true);
        }
Exemple #7
0
        public async Task <IOAuth2Token> ResponseCode(string pin)
        {
            try
            {
                var client   = new ImgurClient(_clientId, _clientSecret);
                var endpoint = new OAuth2Endpoint(client);
                var token    = await endpoint.GetTokenByPinAsync(pin);

                Debug.WriteLine("test");
                return(token);
            }
            catch (ImgurException imgurEx)
            {
                Debug.WriteLine("An error occurred will authentification: " + imgurEx.Message);
                return(null);
            }
        }
Exemple #8
0
        public async Task GetTokenByPinAsync_WithClientSecretNull_ThrowsArgumentNullException()
        {
            var client   = new ImgurClient("123");
            var endpoint = new OAuth2Endpoint(client);

            var exception =
                await
                Record.ExceptionAsync(
                    async() => await endpoint.GetTokenByPinAsync("1234").ConfigureAwait(false))
                .ConfigureAwait(false);

            Assert.NotNull(exception);
            Assert.IsType <ArgumentException>(exception);

            var argNullException = (ArgumentException)exception;

            Assert.Equal(argNullException.Message, "ApiClient.ClientSecret is required.");
        }
        public async Task <bool> Login(string pin)
        {
            try {
                IOAuth2Token token = await _endpoint.GetTokenByPinAsync(pin);

                _client.SetOAuth2Token(token);

                ConfigHelper.WriteRefreshToken(token.RefreshToken);

                User    = token.AccountUsername;
                UserUrl = $"http://{User}.imgur.com/all/";

                _success.Show(string.Format(strings.loggedIn, User), TimeSpan.FromSeconds(2));
                return(true);
            } catch (Exception ex) {
                _error.Show(string.Format(strings.wrongPin, ex.Message), TimeSpan.FromSeconds(2));
                return(false);
            }
        }
Exemple #10
0
        public async Task GetTokenByPinAsync_Equal()
        {
            var mockUrl      = "https://api.imgur.com/oauth2/token";
            var mockResponse = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(MockOAuth2EndpointResponses.GetTokenByPin)
            };

            var client   = new ImgurClient("123", "1234");
            var endpoint = new OAuth2Endpoint(client, new HttpClient(new MockHttpMessageHandler(mockUrl, mockResponse)));
            var token    = await endpoint.GetTokenByPinAsync("4839").ConfigureAwait(false);

            Assert.Equal("PinResponse", token.AccessToken);
            Assert.Equal("2132d34234jkljj84ce0c16fjkljfsdfdc70", token.RefreshToken);
            Assert.Equal("bearer", token.TokenType);
            Assert.Equal(2419200, token.ExpiresIn);
            Assert.Equal("Bob", token.AccountUsername);
            Assert.Equal("45344", token.AccountId);
        }
        public async Task GetTokenByPinAsync_AreEqual()
        {
            var fakeHttpMessageHandler = new FakeHttpMessageHandler();
            var fakeResponse           = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(OAuth2EndpointResponses.OAuth2TokenPinResponse)
            };

            fakeHttpMessageHandler.AddFakeResponse(new Uri("https://api.imgur.com/oauth2/token"), fakeResponse);

            var client   = new ImgurClient("123", "1234");
            var endpoint = new OAuth2Endpoint(client, new HttpClient(fakeHttpMessageHandler));
            var token    = await endpoint.GetTokenByPinAsync("4839");

            Assert.AreEqual("PinResponse", token.AccessToken);
            Assert.AreEqual("2132d34234jkljj84ce0c16fjkljfsdfdc70", token.RefreshToken);
            Assert.AreEqual("bearer", token.TokenType);
            Assert.AreEqual(2419200, token.ExpiresIn);
            Assert.AreEqual("Bob", token.AccountUsername);
            Assert.AreEqual("45344", token.AccountId);
        }
 public async Task GetTokenByPinAsync_WithPinNull_ThrowsArgumentNullException()
 {
     var client   = new ImgurClient("123", "1234");
     var endpoint = new OAuth2Endpoint(client);
     await endpoint.GetTokenByPinAsync(null);
 }