/// <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); appRegistrationConfig.Authority = "https://login.microsoftonline.com/" + appRegistrationConfig.Tenant; }
/// <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); }
internal static async Task <CustomerInsights> ConnectAsync() { AppRegistrationConfig appRegistrationConfig = new AppRegistrationConfig(); ApiRegistrationConfig apiRegistrationConfig = new ApiRegistrationConfig(); LoadConfigs(appRegistrationConfig, apiRegistrationConfig); IConfidentialClientApplication app = BuildClientApplication(appRegistrationConfig); CustomerInsights customerInsightsApi = await CreateCustomerInsightsApi(apiRegistrationConfig, app); api = customerInsightsApi; return(customerInsightsApi); //var sampleInteractions = new SampleApiInteractions(customerInsightsApi); //await sampleInteractions.StartAsync(); }