internal static string GetEntitySetName(HttpClient httpClient, string entityLogicalName) { string requestUri = $"EntityDefinitions(LogicalName='{entityLogicalName}')?$select=EntitySetName"; var response = httpClient.GetAsync(requestUri).GetAwaiter().GetResult(); if (response.IsSuccessStatusCode) { string content = response.Content?.ReadAsStringAsync().GetAwaiter().GetResult(); EntityDefinition entityDefinition = JSONSerializer <EntityDefinition> .DeSerialize(content); return(entityDefinition?.EntitySetName); } else { string content = response.Content?.ReadAsStringAsync().GetAwaiter().GetResult();; throw new InvalidOperationException($"Missing entityset name for entity:{entityLogicalName}. Error Details:{content}"); } }
private void GetOAuthToken(WebApiConfiguration configuration) { //Note that an Azure AD access token has finite lifetime, default expiration is 60 minutes. string tokenEndpoint = string.Format("https://login.microsoftonline.com/{0}/oauth2/token", configuration.TenantId); client.DefaultRequestHeaders.Add("Accept", "application/json"); string requestBody = string.Format("grant_type={0} &resource={1}&client_id={2}&client_secret={3}", "client_credentials", configuration.RedirectUrl, configuration.ClientId, configuration.ClientSecret); HttpResponseMessage response = client.PostAsync(tokenEndpoint, new StringContent(requestBody, Encoding.UTF8, "application/x-www-form-urlencoded")).Result; string result = response.Content.ReadAsStringAsync().Result; if (response.IsSuccessStatusCode) { AuthToken = JSONSerializer <OAuthToken> .DeSerialize(result); } else { throw new HttpRequestException($"Unable to generate Authentication Token for reason:{response.ReasonPhrase}. with exeption: {result}"); } }