public async Task <TokenDetails> GetTokenByRefreshToken()
        {
            //TODO : It returns badRequest. Need to fix it
            var content = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair <string, string>("client_id", config.Value.ClientId)
                , new KeyValuePair <string, string>("client_secret", config.Value.ClientSecret)
                , new KeyValuePair <string, string>("grant_type", "refresh_token")
                , new KeyValuePair <string, string>("refresh_token", tokenStorage.Get <TokenDetails>(config.Value.TokenKey).RefreshToken)
            });


            var client   = httpHelpers.GetClient(config.Value.OAuthServerUrl);
            var response = await client.PostAsync("/connect/token", content);

            if (response.IsSuccessStatusCode)
            {
                dynamic result             = JsonConvert.DeserializeObject <dynamic>(await response.Content.ReadAsStringAsync());
                var     jwtSecurityHandler = new JwtSecurityTokenHandler();
                var     user = (JwtSecurityToken)jwtSecurityHandler.ReadJwtToken(result.access_token.ToString());

                var tokens = new TokenDetails
                {
                    AccessToken  = result.access_token,
                    RefreshToken = result.refresh_token,
                    ExpiryDate   = FromUnixTime(user.Claims.Where(x => x.Type == "exp").FirstOrDefault().Value)
                };

                tokenStorage.Clear();
                tokenStorage.Store(tokens, config.Value.TokenKey);
                return(tokens);
            }
            return(new TokenDetails());
        }
Exemple #2
0
        public async Task <List <AccountModel> > GetAllBankAccounts()
        {
            var token = tokenStorage.Get <TokenDetails>(config.Value.TokenKey);

            var client    = httpHelpers.GetClient(config.Value.ResourceApi);
            var authValue = new AuthenticationHeaderValue("Bearer", token.AccessToken);

            client.DefaultRequestHeaders.Authorization = authValue;

            var response = await client.GetAsync("/data/v1/accounts");

            if (response.IsSuccessStatusCode)
            {
                var result = await response.Content.ReadAsStringAsync();

                var jObject = JObject.Parse(result);

                return(jObject["results"].ToObject <List <AccountModel> >());
            }
            return(new List <AccountModel>());
        }
        public async Task <List <TransactionModel> > GetTransactionByAccountId(string accountId)
        {
            var token = tokenStorage.Get <TokenDetails>(config.Value.TokenKey);

            var client = httpHelpers.GetClient(config.Value.ResourceApi);

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.AccessToken);

            var path = $"/data/v1/accounts/{accountId}/transactions?from=''&to=''";

            var response = await client.GetAsync(path);

            if (response.IsSuccessStatusCode)
            {
                var result = await response.Content.ReadAsStringAsync();

                var jObject = JObject.Parse(result);

                return(jObject["results"].ToObject <List <TransactionModel> >());
            }
            return(new List <TransactionModel>());
        }