public async Task <ActionResult> OAuthCallback(string code, string state) { // verify anti-CSRF state token matches what was sent string expectedState = Session["state"] as string; if (!string.Equals(expectedState, state)) { throw new SecurityException("State mismatch"); } Session.Remove("state"); // exchange authorization code for access token AccessToken accessToken = await _mondoAuthorizationClient.GetAccessTokenAsync(code, Url.Action("OAuthCallback", "Home", null, Request.Url.Scheme)); // fetch transactions etc using (var client = new MondoClient(accessToken.Value, "https://production-api.gmon.io")) { IList <Account> accounts = await client.GetAccountsAsync(); Balance balance = await client.GetBalanceAsync(accounts[0].Id); IList <Transaction> transactions = await client.GetTransactionsAsync(accounts[0].Id, expand : "merchant"); return(View(new AccountSummaryModel { Account = accounts[0], Balance = balance, Transactions = transactions })); } }
public async void GetBalance() { using (var server = TestServer.Create(app => { app.Run(async context => { Assert.AreEqual("/balance?account_id=1", context.Request.Uri.PathAndQuery); Assert.AreEqual("Bearer testAccessToken", context.Request.Headers["Authorization"]); await context.Response.WriteAsync( @"{ 'balance': 5000, 'currency': 'GBP', 'spend_today': -100 }" ); }); })) { using (var client = new MondoClient(server.HttpClient, "testAccessToken")) { var balance = await client.GetBalanceAsync("1"); Assert.AreEqual(5000, balance.Value); Assert.AreEqual("GBP", balance.Currency); Assert.AreEqual(-100, balance.SpendToday); } } }
private async void Login() { try { if (string.IsNullOrWhiteSpace(_loginPageViewModel.Username) || string.IsNullOrWhiteSpace(_loginPageViewModel.Password)) { _loginPageViewModel.ErrorMessage = "Please enter your username and password."; return; } _loginPageViewModel.ErrorMessage = null; _loginPageViewModel.IsEnabled = false; _loginPageViewModel.IsBusy = true; _loginPageViewModel.StatusText = "Authenticating..."; _loginPageViewModel.AccessToken = await _mondoAuthorizationClient.AuthenticateAsync(_loginPageViewModel.Username, _loginPageViewModel.Password); using (var mondoClient = new MondoClient(_loginPageViewModel.AccessToken.Value, "https://production-api.gmon.io")) { ScheduleAccessTokenRefresh(); _loginPageViewModel.StatusText = "Fetching accounts..."; IList <Account> accounts = await mondoClient.GetAccountsAsync(); _loginPageViewModel.StatusText = "Fetching balance..."; Balance balance = await mondoClient.GetBalanceAsync(accounts[0].Id); _loginPageViewModel.StatusText = "Fetching transactions..."; IList <Transaction> transactions = await mondoClient.GetTransactionsAsync(accounts[0].Id, expand : "merchant"); _accountPageViewModel.AccountName = accounts[0].Description; _accountPageViewModel.Balance = balance.Value / 100m; _accountPageViewModel.SpentToday = Math.Abs(balance.SpendToday / 100m); foreach (Transaction transaction in transactions.OrderByDescending(t => t.Created)) { var transactionViewModel = new TransactionViewModel(); transactionViewModel.Amount = transaction.Amount / 100m; transactionViewModel.ImageUrl = transaction.Merchant?.Logo; transactionViewModel.Description = transaction.Merchant?.Name ?? transaction.Description; _accountPageViewModel.Transactions.Add(transactionViewModel); } _navigationService.NavigateTo <AccountSummaryPage>(_accountPageViewModel); _loginPageViewModel.Password = null; _loginPageViewModel.Username = null; } } catch (Exception ex) { _loginPageViewModel.ErrorMessage = ex.Message; } finally { _loginPageViewModel.StatusText = null; _loginPageViewModel.IsBusy = false; _loginPageViewModel.IsEnabled = true; } }