async public void getToken()
        {
            var client_account = new ImgurClient(client_id, client_secret);
            var endpoint       = new OAuth2Endpoint(client_account);

            imgur_token = await endpoint.GetTokenByCodeAsync(code);
        }
Ejemplo n.º 2
0
 public async Task AuthorizationRequest()
 {
     var client           = new ImgurClient(_clientId);
     var endpoint         = new OAuth2Endpoint(client);
     var authorizationUrl = endpoint.GetAuthorizationUrl(OAuth2ResponseType.Pin);
     await Windows.System.Launcher.LaunchUriAsync(new Uri(authorizationUrl));
 }
        public async Task setImgurToken(string _code)
        {
            var client_account = new ImgurClient(client_id, client_secret);
            var endpoint       = new OAuth2Endpoint(client_account);

            imgur_token = await endpoint.GetTokenByCodeAsync(_code);
        }
Ejemplo n.º 4
0
        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;
        }
Ejemplo n.º 5
0
        public async static Task ImgurSetup()
        {
            ApiSettings settings = GetApiLogin();

            Client = new ImgurClient(settings.ClientId, settings.ClientSecret);
            var          endpoint = new OAuth2Endpoint(Client);
            IOAuth2Token token    = null;

            while (token == null)
            {
                try
                {
                    token = await endpoint.GetTokenByRefreshTokenAsync(settings.RefreshToken);
                } catch (Exception ex)
                {
                    token = null;
                    Console.WriteLine(ex.Message);
                    await Task.Delay(5000);
                }
                break;
            }

            Client.SetOAuth2Token(token);

            Console.WriteLine("Imgur Ready.");
        }
Ejemplo n.º 6
0
        public void AskToken()
        {
            var endpoint         = new OAuth2Endpoint(Imgur);
            var authorizationUrl = endpoint.GetAuthorizationUrl(OAuth2ResponseType.Pin);

            System.Diagnostics.Process.Start(authorizationUrl);
        }
Ejemplo n.º 7
0
        private void GetAndSetOAuth2Token()
        {
            var apiClient      = GetApiClientWithKeyAndSecret();
            var oAuth2Endpoint = new OAuth2Endpoint(apiClient, new HttpClient());

            _oAuth2Token = Task.Run(() => oAuth2Endpoint.GetTokenAsync(_refreshToken)).Result;
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
0
        public void GetAuthorizationUrl_SetState_Equal()
        {
            var apiClient = new ApiClient("abc", "ioa");
            var endpoint  = new OAuth2Endpoint(apiClient, new HttpClient());
            var expected  = "https://api.imgur.com/oauth2/authorize?client_id=abc&response_type=token&state=test";

            Assert.Equal(expected, endpoint.GetAuthorizationUrl("test"));
        }
        public IActionResult LoginImgurProfile()
        {
            var client           = new ImgurClient(ClientIdImgur);
            var endpoint         = new OAuth2Endpoint(client);
            var authorizationUrl = endpoint.GetAuthorizationUrl(OAuth2ResponseType.Token);

            return(this.Redirect(authorizationUrl));
        }
        public void GetAuthorizationUrl_SetStateNull_AreEqual()
        {
            var client   = new ImgurClient("xyz", "deb");
            var endpoint = new OAuth2Endpoint(client);
            var expected = "https://api.imgur.com/oauth2/authorize?client_id=xyz&response_type=Code&state=";

            Assert.AreEqual(expected, endpoint.GetAuthorizationUrl(OAuth2ResponseType.Code, null));
        }
        public void GetAuthorizationUrl_SetState_AreEqual()
        {
            var client   = new ImgurClient("abc", "ioa");
            var endpoint = new OAuth2Endpoint(client);
            var expected = "https://api.imgur.com/oauth2/authorize?client_id=abc&response_type=Code&state=test";

            Assert.AreEqual(expected, endpoint.GetAuthorizationUrl(OAuth2ResponseType.Code, "test"));
        }
Ejemplo n.º 13
0
        public void GetAuthorizationUrl_SetStateNull_Equal()
        {
            var apiClient = new ApiClient("xyz", "deb");
            var endpoint  = new OAuth2Endpoint(apiClient, new HttpClient());
            var expected  = "https://api.imgur.com/oauth2/authorize?client_id=xyz&response_type=token&state=";

            Assert.Equal(expected, endpoint.GetAuthorizationUrl());
        }
Ejemplo n.º 14
0
        public static string GetAuthorizationUrl()
        {
            var client           = new ImgurClient(ClientIdImgur);
            var endpoint         = new OAuth2Endpoint(client);
            var authorizationUrl = endpoint.GetAuthorizationUrl(OAuth2ResponseType.Token);

            return(authorizationUrl);
        }
Ejemplo n.º 15
0
        public IActionResult LoginImgUr()
        {
            var client           = new ImgurClient(this.imgUrUploadSettings.ClientID, this.imgUrUploadSettings.ClientSecret);
            var endpoint         = new OAuth2Endpoint(client);
            var authorizationUrl = endpoint.GetAuthorizationUrl(OAuth2ResponseType.Token);

            return(Ok(new { data = authorizationUrl }));
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Login to Imgur with OAuth2
        /// </summary>
        public ImgurLoginHelper(Toasty errorToast, Toasty successToast)
        {
            _client   = new ImgurClient(ClientID, ClientSecret);
            _endpoint = new OAuth2Endpoint(_client);

            error   = errorToast;
            success = successToast;
        }
Ejemplo n.º 17
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);
        }
Ejemplo n.º 18
0
        public static async Task ConnectImgurApi()
        {
            var client = new ImgurClient("3f8dfa6f3541120");

            var endpoint         = new OAuth2Endpoint(client);
            var authorizationUrl = endpoint.GetAuthorizationUrl(OAuth2ResponseType.Pin);

            await Windows.System.Launcher.LaunchUriAsync(new Uri(authorizationUrl));
        }
Ejemplo n.º 19
0
 /**
  * Initialise Variable that permit access to the user account.
  */
 public ImgurApi()
 {
     this.client         = new ImgurClient("76c0f5a4f7e671e", "fd6a836ec1237a2ba383941ca420b2c9bd63922f");
     this.oAuth2Endpoint = new OAuth2Endpoint(client);
     this.client.SetOAuth2Token(token);
     this.imageEndpoint   = new ImageEndpoint(client);
     this.accountEndpoint = new AccountEndpoint(client);
     this.galleryEndpoint = new GalleryEndpoint(client);
 }
Ejemplo n.º 20
0
        public static string GetAuthorizationUrl()
        {
            ApiSettings settings = GetApiLogin();

            Client = new ImgurClient(settings.ClientId, settings.ClientSecret);
            var endpoint = new OAuth2Endpoint(Client);

            return(endpoint.GetAuthorizationUrl(Imgur.API.Enums.OAuth2ResponseType.Token));
        }
        public void RefreshToken()
        {
            var client           = new ImgurClient(MainPage.CLIENT_ID, MainPage.CLIENT_SECRET);
            var endpoint         = new OAuth2Endpoint(client);
            var NEW_ACCESS_TOKEN = endpoint.GetTokenByRefreshTokenAsync(REFRESH_TOKEN);

            Application.Current.Properties["TOKEN_ACCESS"] = NEW_ACCESS_TOKEN;

            token = new OAuth2Token(NEW_ACCESS_TOKEN.ToString(), REFRESH_TOKEN, TOKEN_TYPE, ACCOUNT_ID, IMGUR_USER_ACCOUNT, int.Parse(EXPIRES_IN));
        }
Ejemplo n.º 22
0
        public string AuthUser()
        {
            IniData authData = null;
            string  clientId = null;
            // Create a parser to read the auth.ini file
            var parser = new FileIniDataParser();

            try {
                authData = parser.ReadFile(Environment.ExpandEnvironmentVariables(Environment.ExpandEnvironmentVariables("%appdata%\\ShareShot\\auth.ini")));
                clientId = authData["credentials"]["client_id"];
            }
            catch (ParsingException) {
                Console.WriteLine("Error: File not found.");
                return(null);
            }

            // Get the authorization url using the clientId
            var client           = new ImgurClient(clientId);
            var endpoint         = new OAuth2Endpoint(client);
            var authorizationUrl = endpoint.GetAuthorizationUrl(OAuth2ResponseType.Token);

            // Create a web driver that will open the authorization url in an instance of Firefox
            IWebDriver driver = new FirefoxDriver {
                Url = authorizationUrl
            };

            // Wait until the user logs in and the browser redirects
            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromDays(365));

            wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.UrlContains("#access_token="));

            // Get the url of the redirected web page
            string url = driver.Url;

            // Close the IWebDriver
            driver.Close();

            // The url comes with a substring that needs to be deleted
            url = url.Replace("state=#", "");

            // Create a uri using the updated url, then turn it into a functional string that can be queried
            Uri    uri         = new Uri(url);
            string queryString = uri.Query;

            // Get the access and refresh tokens
            string accessToken  = System.Web.HttpUtility.ParseQueryString(queryString).Get("access_token");
            string refreshToken = System.Web.HttpUtility.ParseQueryString(queryString).Get("refresh_token");

            authData["credentials"]["refresh_token"] = refreshToken;
            parser.WriteFile(Environment.ExpandEnvironmentVariables("%appdata%\\ShareShot\\auth.ini"), authData);

            return(accessToken);
        }
        public async Task GetTokenByRefreshTokenAsync_SetToken_IsNotNull()
        {
            var authentication = new ImgurClient(ClientId, ClientSecret);
            var endpoint       = new OAuth2Endpoint(authentication);
            var token          = await endpoint.GetTokenByRefreshTokenAsync(RefreshToken);

            Assert.IsNotNull(token);
            Assert.IsFalse(string.IsNullOrWhiteSpace(token.AccessToken));
            Assert.IsFalse(string.IsNullOrWhiteSpace(token.RefreshToken));
            Assert.IsFalse(string.IsNullOrWhiteSpace(token.AccountId));
            Assert.IsFalse(string.IsNullOrWhiteSpace(token.TokenType));
        }
Ejemplo n.º 24
0
        private IOAuth2Token GetOAuth2Token()
        {
            if (_token != null)
            {
                return(_token);
            }

            var authentication = new ImgurClient(ClientId, ClientSecret);
            var endpoint       = new OAuth2Endpoint(authentication);

            _token = endpoint.GetTokenByRefreshTokenAsync(RefreshToken).Result;
            return(_token);
        }
Ejemplo n.º 25
0
        //Login to Imgur Account
        private async void Login()
        {
            try {
                OAuth2Endpoint endpoint = new OAuth2Endpoint(_client);

                string       refreshToken = FileIO.ReadRefreshToken();
                IOAuth2Token token        = await endpoint.GetTokenByRefreshTokenAsync(refreshToken);

                _client.SetOAuth2Token(token);
            } catch {
                // ignored
            }
        }
Ejemplo n.º 26
0
        public async Task GetTokenAsync_WithClientSecretNull_ThrowsInvalidOperationException()
        {
            var apiClient = new ApiClient("123");
            var endpoint  = new OAuth2Endpoint(apiClient, new HttpClient());

            var exception = await Record.ExceptionAsync(async() =>
            {
                await endpoint.GetTokenAsync("1234");
            });

            Assert.NotNull(exception);
            Assert.IsType <InvalidOperationException>(exception);
        }
Ejemplo n.º 27
0
        public async Task GetTokenByRefreshTokenAsync_WithTokenNull_ThrowsArgumentNullException()
        {
            var client   = new ImgurClient("123", "1234");
            var endpoint = new OAuth2Endpoint(client);

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

            Assert.NotNull(exception);
            Assert.IsType <ArgumentNullException>(exception);
        }
        public async Task GetTokenByCodeAsync_ThrowsImgurException()
        {
            var fakeHttpMessageHandler = new FakeHttpMessageHandler();
            var fakeResponse           = new HttpResponseMessage(HttpStatusCode.BadRequest)
            {
                Content = new StringContent(OAuth2EndpointResponses.OAuth2TokenResponseError)
            };

            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));
            await endpoint.GetTokenByCodeAsync("12345");
        }
Ejemplo n.º 29
0
        //Login to Imgur Account
        public async Task Login()
        {
            try {
                string refreshToken = ConfigHelper.ReadRefreshToken();
                if (string.IsNullOrWhiteSpace(refreshToken))
                {
                    return;
                }

                OAuth2Endpoint endpoint = new OAuth2Endpoint(_client);
                IOAuth2Token   token    = await endpoint.GetTokenByRefreshTokenAsync(refreshToken);

                _client.SetOAuth2Token(token);
            } catch {
                // ignored
            }
        }
Ejemplo n.º 30
0
        private async Task <ImgurToken> CreateNewToken(ApiClient apiClient, HttpClient httpClient)
        {
            OAuth2Endpoint oAuth2Endpoint = new OAuth2Endpoint(apiClient, httpClient);
            // Generate url token: oAuth2Endpoint.GetAuthorizationUrl();
            var token = await oAuth2Endpoint.GetTokenAsync(this._imgurConfigData.RefreshToken);

            ImgurToken imgurToken = new ImgurToken
            {
                Token        = token.AccessToken,
                ExpiresIn    = token.ExpiresIn,
                CreationTime = DateTime.Now,
            };

            this._repository.Add(imgurToken);

            return(imgurToken);
        }