/// <summary> /// Requests a fully formed IXeroToken with list of tenants filled /// </summary> /// <param name="code">Code returned from callback</param> /// <returns></returns> public async Task <IXeroToken> RequestAccessTokenAsync(string code) { var response = await _httpClient.RequestAuthorizationCodeTokenAsync(new AuthorizationCodeTokenRequest { Address = "https://identity.xero.com/connect/token", GrantType = "code", Code = code, ClientId = xeroConfiguration.ClientId, ClientSecret = xeroConfiguration.ClientSecret, RedirectUri = xeroConfiguration.CallbackUri.AbsoluteUri, Parameters = { { "scope", xeroConfiguration.Scope } } }); if (response.IsError) { throw new Exception(response.Error); } var xeroToken = new XeroOAuth2Token() { AccessToken = response.AccessToken, RefreshToken = response.RefreshToken, ExpiresAtUtc = DateTime.UtcNow.AddSeconds(response.ExpiresIn), IdToken = response.IdentityToken, }; xeroToken.Tenants = await GetConnectionsAsync(xeroToken); return(xeroToken); }
/// <summary> /// Requests a fully formed IXeroToken with list of tenants filled /// </summary> /// <returns></returns> public async Task <IXeroToken> RequestClientCredentialsTokenAsync(bool fetchTenants = true) { var response = await _httpClient.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest { Address = "https://identity.xero.com/connect/token", ClientId = xeroConfiguration.ClientId, ClientSecret = xeroConfiguration.ClientSecret, Scope = xeroConfiguration.Scope }); if (response.IsError) { throw new Exception(response.Error); } var xeroToken = new XeroOAuth2Token() { AccessToken = response.AccessToken, ExpiresAtUtc = DateTime.UtcNow.AddSeconds(response.ExpiresIn) }; if (fetchTenants) { xeroToken.Tenants = await GetConnectionsAsync(xeroToken); } return(xeroToken); }
public async Task StoreToken(XeroOAuth2Token xeroToken) { await Task.Run(() => { var serializedXeroToken = JsonConvert.SerializeObject(xeroToken); File.WriteAllText(_serializedXeroTokenPath, serializedXeroToken); }); }
public static void StoreToken(XeroOAuth2Token xeroToken) { string serializedXeroToken = JsonSerializer.Serialize(xeroToken); string currentPath = AppDomain.CurrentDomain.BaseDirectory; string filePath = currentPath + "xerotoken.json"; System.IO.File.WriteAllText(filePath, serializedXeroToken); }
public async Task StoreToken(XeroOAuth2Token xeroToken) { var records = xeroToken.Tenants .Select(t => { var token = xeroToken.IsOrMap <Functions.Authorization.Token.Token>(); token.Tenant = t; // new[] { t }.ToList(); // reset to store only one tenant details return(token.CreatTableEntity(nameof(PartitionKeyValue.Xero), t.TenantId.ToString())); }); await records.UpdateIn(_table); }
public static XeroOAuth2Token GetStoredToken() { var xeroToken = new XeroOAuth2Token(); try { string serializedXeroToken = System.IO.File.ReadAllText(Application.UserAppDataPath + "./xerotoken.json"); xeroToken = JsonSerializer.Deserialize <XeroOAuth2Token>(serializedXeroToken); return(xeroToken); } catch (Exception) { } return(xeroToken); }
private static Func <TokenValidatedContext, Task> OnTokenValidated() { return(context => { var tokenStore = context.HttpContext.RequestServices.GetService <MemoryTokenStore>(); var token = new XeroOAuth2Token { AccessToken = context.TokenEndpointResponse.AccessToken, RefreshToken = context.TokenEndpointResponse.RefreshToken, ExpiresAtUtc = DateTime.UtcNow.AddSeconds(Convert.ToDouble(context.TokenEndpointResponse.ExpiresIn)) }; tokenStore.SetToken(context.Principal.XeroUserId(), token); return Task.CompletedTask; }); }
public async Task StoreToken(XeroOAuth2Token xeroToken) { var operations = xeroToken.Tenants .Select(t => { var token = xeroToken.IsOrMap <Token>(); token.Tenant = t; // new[] { t }.ToList(); // reset to store only one tenant details token.Id = t.TenantId.ToString(); token.Pk = nameof(PartitionKeyValue.Xero); return(_container.UpsertItemAsync(token)); }); await Task.WhenAll(operations); //await _container.CreateItemAsync(xeroToken, new PartitionKey("xero")); }
public async Task Create(XeroOAuth2Token xeroToken) { await using var fs = File.Create(XeroTokenPath); await JsonSerializer.SerializeAsync(fs, xeroToken); }
public static void StoreToken(XeroOAuth2Token xeroToken) { string serializedXeroToken = JsonSerializer.Serialize(xeroToken); System.IO.File.WriteAllText("./xerotoken.json", serializedXeroToken); }
public void Run(string name) { try { Console.WriteLine(name + ": Starting "); var clientId = _config["ClientId"]; XeroClient client = null; IXeroToken token = null; lock (_lockObj) { client = new XeroClient(new XeroConfiguration { ClientId = clientId }); if (System.IO.File.Exists("Token.json")) { var savedJson = System.IO.File.ReadAllText("Token.json"); token = JsonConvert.DeserializeObject <XeroOAuth2Token>(savedJson); } if (token == null) { token = new XeroOAuth2Token { RefreshToken = _config["RefreshToken"] } } ; IXeroToken newToken = Task.Run(() => client.RefreshAccessTokenAsync(token)).GetAwaiter().GetResult(); if (newToken != null) { token = newToken; } var json = JsonConvert.SerializeObject(token, Formatting.Indented); System.IO.File.WriteAllText("Token.json", json); } Console.WriteLine(name + ": Token refreshed"); var accessToken = token.AccessToken; var tenant = ""; try { var conRes = Task.Run(() => client.GetConnectionsAsync(token)).GetAwaiter().GetResult(); tenant = conRes[0].TenantId.ToString(); Console.WriteLine(name + ": Tenants"); } catch (Exception e) { Console.WriteLine(e); SerializeException(e); return; } var api = new AccountingApi { ExceptionFactory = CustomExceptionFactory }; Console.WriteLine(name + ": New token process"); var response = Task.Run(() => api.GetInvoicesAsyncWithHttpInfo(accessToken, tenant)).GetAwaiter().GetResult(); var respInvoices = Task.Run(() => api.GetInvoicesAsyncWithHttpInfo(accessToken, tenant, page: 1, where : "")).GetAwaiter().GetResult(); var invDate = respInvoices.Data._Invoices[0].Date; var recurring = Task.Run(() => api.GetRepeatingInvoicesAsyncWithHttpInfo(accessToken, tenant)).GetAwaiter().GetResult(); var status = recurring.Data._RepeatingInvoices[0].Status; var schedule = recurring.Data._RepeatingInvoices[0].Schedule; Console.WriteLine(name + ": Complete"); } catch (CustomApiException ce) { Console.WriteLine(name + ": Failed: " + ce.Message); SerializeException(ce); Console.WriteLine(ce); var limits = new XeroLimits(ce.Headers); } catch (Exception e) { Console.WriteLine(name + ": Failed: " + e.Message); SerializeException(e); } }