async Task <bool> PerformOauth() { const string redirect = "https://api-sandbox.oanda.com/index.html"; const string AccountId = "g6E5XbvB28Ph9Kin"; const string scopes = "read+trade+marketdata+stream"; // create a random state string var rand = new Random(); string state = "fnuajuiwqnzpofmAHAAJfNMLKAOW9mvm9r201jmpmfpa" + rand.Next() + rand.Next() + rand.Next(); //String GoogleURL = "https://accounts.google.com/o/oauth2/auth?Account_id=" + Uri.EscapeDataString(GoogleAccountID.Text) + "&redirect_uri=" + Uri.EscapeDataString(GoogleCallbackUrl.Text) + "&response_type=code&scope=" + Uri.EscapeDataString("http://picasaweb.google.com/data"); string requestURL = "https://api-fxpractice.oanda.com/v1/oauth2/authorize?Account_id=" + Uri.EscapeDataString(AccountId) + "&redirect_uri=" + Uri.EscapeDataString(redirect) + "&state=" + Uri.EscapeDataString(state) + "&response_type=token&scope=" + Uri.EscapeDataString(scopes); Uri startUri = new Uri(requestURL); // When using the desktop flow, the success code is displayed in the html title of this end uri Uri EndUri = new Uri(redirect + "#"); WebAuthenticationResult WebAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync( WebAuthenticationOptions.None, startUri, EndUri); if (WebAuthenticationResult.ResponseStatus == WebAuthenticationStatus.Success) { Regex stateRegex = new Regex("state=([^&]*)"); Regex tokenRegex = new Regex("access_token=([^&]*)"); string returnedState = stateRegex.Match(WebAuthenticationResult.ResponseData).Groups[1].ToString(); string returnedToken = tokenRegex.Match(WebAuthenticationResult.ResponseData).Groups[1].ToString(); if (returnedState != state) { // freak out or something throw new Exception("Unexpected state returned during authentication"); } Credentials.SetCredentials(EEnvironment.Practice, returnedToken); // Set the default account var accounts = await Rest.GetAccountListAsync(); Credentials.SetCredentials(EEnvironment.Practice, returnedToken, accounts[0].accountId); return(true); } else if (WebAuthenticationResult.ResponseStatus == WebAuthenticationStatus.ErrorHttp) { // Note: Should do something useful with this error // "HTTP Error returned by AuthenticateAsync() : " + WebAuthenticationResult.ResponseErrorDetail.ToString(); return(false); } else { // Note: Should do something useful with this error // return "Error returned by AuthenticateAsync() : " + WebAuthenticationResult.ResponseStatus.ToString(); return(false); } }
private async Task RunAccountsTest() { // Get Account List List <Account> result; if (Credentials.GetDefaultCredentials().IsSandbox) { result = await Rest.GetAccountListAsync(Credentials.GetDefaultCredentials().Username); } else { result = await Rest.GetAccountListAsync(); } _results.Verify(result.Count > 0, "Accounts are returned"); foreach (var account in result) { _results.Verify(VerifyDefaultData(account), "Checking account data for " + account.accountId); // Get Account Information var accountDetails = await Rest.GetAccountDetailsAsync(account.accountId); _results.Verify(VerifyAllData(accountDetails), "Checking account details data for " + account.accountId); } }