示例#1
0
        /// <summary>
        /// NuInvest has a validation against bots, so the login for this provider is done by browser simulation
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="userPassword"></param>
        /// <returns></returns>
        private async Task Authenticate(string userName, string userPassword, string optCode = null)
        {
            var browser = await Puppeteer.LaunchAsync(new LaunchOptions
            {
                Headless = false,
            });

            using var page = await browser.NewPageAsync();

            await page.SetRequestInterceptionAsync(true);

            page.Request += async(sender, e) =>
            {
                var authenticationRequest = new AuthenticationRequest(userName, userPassword, optCode);
                var payload = new Payload
                {
                    Method   = HttpMethod.Post,
                    Headers  = authenticationRequest.GetHeaders(),
                    PostData = authenticationRequest.BuildFormValues(),
                };
                await e.Request.ContinueAsync(payload);
            };
            var response = await page.GoToAsync(ProviderUrls.AUTHENTICATE);

            var responseAuthenticate = await response.JsonAsync <GetAuthenticationResponse>();

            var responseJson = await response.JsonAsync();

            _authToken = responseJson.Value <string>("access_token");

            await browser.CloseAsync();
        }