/// <summary> /// Loads the <see cref="AppRegistrationConfig"/> and <see cref="ApiRegistrationConfig"/> configs. /// </summary> /// <param name="appRegistrationConfig">Config for Azure App Registraiton</param> /// <param name="apiRegistrationConfig">Config for the API Subscription Key from Customer Insights.</param> private static void LoadConfigs(AppRegistrationConfig appRegistrationConfig, ApiRegistrationConfig apiRegistrationConfig) { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json"); IConfigurationRoot configuration = builder.Build(); configuration.GetSection("AppRegistration").Bind(appRegistrationConfig); configuration.GetSection("ApiRegistration").Bind(apiRegistrationConfig); }
/// <summary> /// Creates the Customer Insights API by acquiring an access token from the app and constructing the client with proper HTTP headers. /// </summary> /// <param name="apiRegistrationConfig">The app registration config.</param> /// <param name="app">The app configured with client secrets and authority.</param> /// <returns>An instantiated and authorized Customer Insights API</returns> private static async Task <CustomerInsights> CreateCustomerInsightsApi(ApiRegistrationConfig apiRegistrationConfig, IConfidentialClientApplication app) { var accessToken = await AcquireAccessTokenAsync(app); var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Add(HeaderNames.Authorization, $"Bearer {accessToken}"); httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", apiRegistrationConfig.ApiSubscriptionKey); var customerInsightsApi = new CustomerInsights(httpClient, false); return(customerInsightsApi); }
/// <summary> /// Entry point for the program - creates and loads configs, builds app from app registration for auth, creates Customer Insights API client, and starts interactions. /// </summary> /// <returns>A task for the async main function.</returns> public static async Task Main() { AppRegistrationConfig appRegistrationConfig = new AppRegistrationConfig(); ApiRegistrationConfig apiRegistrationConfig = new ApiRegistrationConfig(); LoadConfigs(appRegistrationConfig, apiRegistrationConfig); IConfidentialClientApplication app = BuildClientApplication(appRegistrationConfig); CustomerInsights customerInsightsApi = await CreateCustomerInsightsApi(apiRegistrationConfig, app); var sampleInteractions = new SampleApiInteractions(customerInsightsApi); await sampleInteractions.StartAsync(); }