Beispiel #1
0
        private async void LoginButtonClick(object sender, EventArgs e)
        {
            // NOTE 1: I've added a WebBrowser control to pass challenge url and verify your account
            // Yout need this because you have to get cookies and document source text from webbrowser
            // and pass it to SetCookiesAndHtmlForChallenge function.

            var userSession = new UserSessionData
            {
                UserName = txtUser.Text,
                Password = txtPass.Text
            };

            InstaApi = InstaApiBuilder.CreateBuilder()
                       .SetUser(userSession)
                       .UseLogger(new DebugLogger(LogLevel.Exceptions))
                       .SetRequestDelay(RequestDelay.FromSeconds(0, 1))
                       .Build();
            Text = $"{AppName} Connecting";
            try
            {
                if (File.Exists(StateFile))
                {
                    Debug.WriteLine("Loading state from file");
                    using (var fs = File.OpenRead(StateFile))
                    {
                        InstaApi.LoadStateDataFromStream(fs);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }

            if (!InstaApi.IsUserAuthenticated)
            {
                var logInResult = await InstaApi.LoginAsync();

                if (!logInResult.Succeeded)
                {
                    if (logInResult.Value == InstaLoginResult.ChallengeRequired)
                    {
                        // Get challenge information
                        var instaChallenge = InstaApi.GetChallenge();
                        IsWebBrowserInUse     = false;
                        WebBrowserRmt.Visible = true;
                        // Navigate to challenge Url
                        WebBrowserRmt.Navigate(instaChallenge.Url);
                        Size = ChallengeSize;
                    }
                }
                else
                {
                    Text = $"{AppName} Connected";
                    // Save session
                    var state = InstaApi.GetStateDataAsStream();
                    using (var fileStream = File.Create(StateFile))
                    {
                        state.Seek(0, SeekOrigin.Begin);
                        state.CopyTo(fileStream);
                    }
                }
            }
            else
            {
                Text = $"{AppName} Connected";
            }
        }