예제 #1
0
        public void OAuth2Token_SetBySetOAuth2Token_Null()
        {
            var oAuth2Token = MockOAuth2Token.GetOAuth2Token();
            var client      = new ImgurClient("ClientId", "ClientSecret");

            Assert.Null(client.OAuth2Token);
            client.SetOAuth2Token(oAuth2Token);
            Assert.Same(oAuth2Token, client.OAuth2Token);
            client.SetOAuth2Token(null);
            Assert.Null(client.OAuth2Token);
        }
예제 #2
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;
        }
예제 #3
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.");
        }
예제 #4
0
        public static async void FavoriteImage(String id, UserInfo user)
        {
            var client = new ImgurClient("3f8dfa6f3541120", "dcb972ae2f6952efb8e9a156fe0c65b8136b3010");

            client.SetOAuth2Token(user.tokenObject);
            var endpoint  = new ImageEndpoint(client);
            var favorited = await endpoint.FavoriteImageAsync(id);
        }
예제 #5
0
        public static async Task <IEnumerable <IGalleryItem> > SearchGallery(String query, UserInfo user)
        {
            var client = new ImgurClient("3f8dfa6f3541120", "dcb972ae2f6952efb8e9a156fe0c65b8136b3010");

            client.SetOAuth2Token(user.tokenObject);
            var endpoint = new GalleryEndpoint(client);
            var images   = await endpoint.SearchGalleryAsync(query);

            return(images);
        }
예제 #6
0
        public static async Task <IEnumerable <IImage> > GetUpl(UserInfo user)
        {
            var client = new ImgurClient("3f8dfa6f3541120", "dcb972ae2f6952efb8e9a156fe0c65b8136b3010");

            client.SetOAuth2Token(user.tokenObject);
            var endpoint = new AccountEndpoint(client);
            var favs     = await endpoint.GetImagesAsync();

            return(favs);
        }
예제 #7
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
            }
        }
예제 #8
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
            }
        }
예제 #9
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);
            }
        }
예제 #10
0
        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);
            }
        }
예제 #11
0
        public static async Task UploadImage(IRandomAccessStream imageLocation, UserInfo user)
        {
            try
            {
                var client = new ImgurClient("3f8dfa6f3541120", "dcb972ae2f6952efb8e9a156fe0c65b8136b3010");
                client.SetOAuth2Token(user.tokenObject);
                var endpoint = new ImageEndpoint(client);

                IImage image;

                image = await endpoint.UploadImageStreamAsync(imageLocation.AsStream());

                Debug.Write("Image uploaded. Image Url: " + image.Link);
            }
            catch (ImgurException imgurEx)
            {
                Debug.Write("An error occurred uploading an image to Imgur.");
                Debug.Write(imgurEx.Message);
            }
        }
예제 #12
0
        //Login to Imgur Account
        public async Task Login()
        {
            try {
                if (_token != null && _token.ExpiresIn > 60)
                {
                    return; // More than 60 seconds left; we don't need to login
                }
                string refreshToken = ConfigHelper.ReadRefreshToken();
                if (string.IsNullOrWhiteSpace(refreshToken)) // No refresh token found; exit
                {
                    throw new Exception("No refresh-token found!");
                }

                // Refresh access token
                OAuth2Endpoint endpoint = new OAuth2Endpoint(_client);
                _token = await endpoint.GetTokenByRefreshTokenAsync(refreshToken);

                _client.SetOAuth2Token(_token);
            } catch {
                // ignored
            }
        }
예제 #13
0
        public async Task <IActionResult> UploadFile(string type = "image")
        {
            await CheckIsSignoutedAsync();

            // Get file Upload from HttpRequest FormData
            FileModel postedFile = Request.File();

            // Check has file
            if (postedFile == null)
            {
                throw new ApiException("File không tồn tại, Vui lòng chọn lại file!", (int)HttpStatusCode.BadRequest);
            }

            // is upload image
            if (type == "image")
            {
                postedFile.Name = "image";
                // Extention image
                if (!AcceptedImageFormats.MineTypes.Contains(postedFile.ContentType.ToLower()))
                {
                    throw new ApiException($"File chỉ hỗ trợ định dạng {string.Join(",", AcceptedImageFormats.MineTypes)}", (int)HttpStatusCode.BadRequest);
                }
                // Photo FileSize Upto 10 MB
                if (postedFile.Length > 10 * 1024 * 1024)
                {
                    throw new ApiException("Dung lượng file ảnh không được vượt quá 10 MB!", (int)HttpStatusCode.BadRequest);
                }
            }
            // is upload video
            else if (type == "video")
            {
                postedFile.Name = "video";
                // Extention Video
                if (!AcceptedVideoFormats.MineTypes.Contains(postedFile.ContentType.ToLower()))
                {
                    throw new ApiException($"File chỉ hỗ trợ định dạng {string.Join(",", AcceptedVideoFormats.MineTypes)}", (int)HttpStatusCode.BadRequest);
                }
                // Photo FileSize Upto 10 MB
                if (postedFile.Length > 200 * 1024 * 1024)
                {
                    throw new ApiException("Dung lượng file video không được vượt quá 200 MB!", (int)HttpStatusCode.BadRequest);
                }
            }
            // not support media type
            else
            {
                throw new ApiException("not support media type", (int)HttpStatusCode.BadRequest);
            }

            // Save postedFile to ImgUr API
            try
            {
                var client = new ImgurClient(this.imgUrUploadSettings.ClientID, this.imgUrUploadSettings.ClientSecret);
                // state=#access_token=d9cb9143c19c67696e26f4ac59b2f7424af7650c&expires_in=315360000&token_type=bearer&refresh_token=ddd479acc0fa1b91b099599f864ac0a9afa82801&account_username=ngnamB&account_id=96736173
                var token = new OAuth2Token(
                    accessToken: "d9cb9143c19c67696e26f4ac59b2f7424af7650c",
                    refreshToken: "ddd479acc0fa1b91b099599f864ac0a9afa82801",
                    tokenType: "bearer",
                    accountId: "96736173",
                    accountUsername: "******",
                    expiresIn: 315360000);
                //var endpoint = new OAuth2Endpoint(client);
                //var token = endpoint.GetTokenByRefreshTokenAsync("REFRESH_TOKEN");
                client.SetOAuth2Token(token);
                var albumEndpoint = new AlbumEndpoint(client);
                // https://imgur.com/a/Dsoqayj
                IAlbum album = await albumEndpoint.GetAlbumAsync("Dsoqayj");

                var    imageEndpoint = new ImageEndpoint(client);
                IImage imageOrVideo  = await imageEndpoint.UploadImageBinaryAsync(image : postedFile.Bytes, albumId : album.Id);

                //Debug.Write("Image retrieved. Image Url: " + image.Link);
                return(Ok(imageOrVideo));
            }
            catch (ImgurException imgurEx)
            {
                _logger.LogError("UploadFile---", imgurEx);
#if !DEBUG
                throw new ApiException("An error occurred getting an image from Imgur.");
#else
                throw new ApiException(imgurEx);
#endif
            }
        }