Пример #1
0
        private async Task Authenticate(string email, string password)
        {
            var authParameters = new Dictionary <string, string>()
            {
                ["email"]    = email,
                ["password"] = password
            };

            var content      = new FormUrlEncodedContent(authParameters);
            var authResponse = await _client.PostAsync("authenticate", content);

            var body = await authResponse.Content.ReadAsStringAsync();

            if (!authResponse.IsSuccessStatusCode)
            {
                throw new InvalidOperationException($"{authResponse.StatusCode}: Unable to authenticate with the sense api. {body}");
            }

            var authResult = JsonSerializer.Deserialize <ApiAuthenticationResponse>(body, _serializerOptions);

            _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
            Account     = AuthenticationResponse.Create(authResult);
            _authResult = authResult;
        }