public Task <string> FetchBalances() { var callbackUrl = Request.Url.ToString(); // retrieve CSRF token from browser cookie var csrfToken = Request.Cookies["csrf_token"]; return(GetPfmMember() // check CSRF token and retrieve state and token ID from callback parameters .FlatMap(mem => tokenClient.ParseTokenRequestCallbackUrl(callbackUrl, csrfToken.Value) // use access token's permissions from now on .Map(callback => mem.ForAccessToken(callback.TokenId))) .FlatMap(representable => representable.GetAccounts()) .FlatMap(async accounts => { // for each account, get its balance var balanceTasks = accounts.Select(account => account.GetBalance(Key.Types.Level.Standard)); var balances = await Task.WhenAll(balanceTasks); // respond to script.js with JSON return "{\"balances\":[" + string.Join(",", balances.Select(bal => JsonConvert.SerializeObject(bal.Current))) + "]}"; })); }