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 GetAccounts() { using (var server = TestServer.Create(app => { app.Run(async context => { Assert.AreEqual("/accounts", context.Request.Uri.PathAndQuery); Assert.AreEqual("Bearer testAccessToken", context.Request.Headers["Authorization"]); await context.Response.WriteAsync( @"{ 'accounts': [ { 'id': 'acc_00009237aqC8c5umZmrRdh', 'description': 'Peter Pan\'s Account', 'created': '2015-11-13T12:17:42Z' } ] }" ); }); })) { using (var client = new MondoClient(server.HttpClient, "testAccessToken")) { var accounts = await client.GetAccountsAsync(); Assert.AreEqual(1, accounts.Count); Assert.AreEqual("acc_00009237aqC8c5umZmrRdh", accounts[0].Id); Assert.AreEqual("Peter Pan's Account", accounts[0].Description); Assert.AreEqual(new DateTime(2015, 11, 13, 12, 17, 42, DateTimeKind.Utc), accounts[0].Created); } } }
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; } }