private static async Task AuthorizeAndSaveTokenAsync() { // Create the TwinfieldApi var twinfieldApi = new TwinfieldApi(oauthClientSettings); // First get the authorization URL Console.WriteLine("Send the user to the following URL:"); Console.WriteLine(twinfieldApi.GetAuthorizationUrl(redirectUrl)); // Catch the authorization code in your own program, and paste it here Console.Write("\n\nEnter the code: "); var authenticationCode = Console.ReadLine(); // Get the access token await twinfieldApi .SetAccessTokenByAuthorizationCodeAsync(authenticationCode, redirectUrl); // Store the access token in KeyVault var accessToken = twinfieldApi.Token; Console.WriteLine("Saving access token to KeyVault.."); await StoreAccesstokenInKeyVaultAsync(accessToken); Console.WriteLine("Done."); }
public static async Task Run() { // Create the TwinfieldApi twinfieldApi = new TwinfieldApi(oauthClientSettings); // You can also provide an HttpClient: // twinfieldApi = new TwinfieldApi(httpClient, oauthClientSettings); // Authorization flow await Example1(); Console.WriteLine($"Auth code: {accessToken.Accesstoken}"); // Instead of authenticating, you can also set the access token twinfieldApi.Token = accessToken; // Check if the token should be refreshed await Example2(); Console.WriteLine($"Auth code: {accessToken.Accesstoken}"); // Always try to catch a TokenExpiredException when calling the API try { // Get offices list and select the first await Example3(); // Get the fields for the balance sheet await Example4(); // Get the fields for profit and loss await Example5(); // Example 6: Get the general ledger request options // Example 7: Get a valid request, including range // Example 9: Reading data from the general ledger var requestOptions = await Example6(); var minimalRequestOptions = Example7(requestOptions); await Example9(minimalRequestOptions); // Example 8: Narrowing down the result of Example #6 even more // Example 9: Reading data from the general ledger minimalRequestOptions = Example8(requestOptions); await Example9(minimalRequestOptions); // Read data from the general ledger, based on the minimum request options. await Example10(); } catch (TokenExpiredException) { // Refresh the token and retry } }
private static async Task LoadTokenFromKeyVaultandRunDemoAsync(bool canRetry = true) { // Create the TwinfieldApi var twinfieldApi = new TwinfieldApi(oauthClientSettings) { Token = await GetAccessTokenFromKeyVaultAsync() }; // Check if the token should be refreshed if (twinfieldApi.Token.IsExpired()) { Console.WriteLine("Token expired, refreshing.."); await twinfieldApi.RefreshTokenAsync(); await StoreAccesstokenInKeyVaultAsync(twinfieldApi.Token); } try { // The token should be good, make some calls to Twinfield now var officeList = await twinfieldApi.ServiceFactory.ProcessXmlDataService.GetOfficeList(); Console.WriteLine($"List of all {officeList.Count} offices:"); foreach (var o in officeList) { Console.WriteLine("{0,10} {1,20} {2}", o.Code, o.ShortName, o.Name); } // And place the rest of your logic here, including extra calls to Twinfield //... //... //... } catch (TokenExpiredException) { if (canRetry) { // Refresh the token await twinfieldApi.RefreshTokenAsync(); await StoreAccesstokenInKeyVaultAsync(twinfieldApi.Token); // Retry (once) await LoadTokenFromKeyVaultandRunDemoAsync(false); } else { // Token could not be refreshed. Throw exception and quit. throw; } } }