Skip to content

Veracity API

CodeGx edited this page Apr 2, 2018 · 1 revision

The following code shows how to call Veracity API. The method verifies if user has accepted the policy of using Veracity platform and service.

More references could be found at the API v3 site.

 private async Task<string> GetServiceSubscription(string serviceId, string userId)
        {
            string responseString = "";
            try
            {
                // Retrieve the token with the specified scopes
                var scope = AzureAdB2COptions.ApiScopes.Split(' ');
                TokenCache userTokenCache = new MSALSessionCache(userId, this.HttpContext).GetMsalCacheInstance();
                ConfidentialClientApplication cca = new ConfidentialClientApplication(AzureAdB2COptions.ClientId, AzureAdB2COptions.Authority, AzureAdB2COptions.RedirectUri, new ClientCredential(AzureAdB2COptions.ClientSecret), userTokenCache, null);

                AuthenticationResult result = await cca.AcquireTokenSilentAsync(scope, cca.Users.FirstOrDefault(), AzureAdB2COptions.Authority, false);

                HttpClient client = new HttpClient();
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, AzureAdB2COptions.ApiUrl + $"/my/policies/{serviceId}/validate()");
                // Add token to the Authorization header and make the request
                request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
                HttpResponseMessage response = await client.SendAsync(request);

                // Handle the response
                switch (response.StatusCode)
                {
                    case HttpStatusCode.OK:
                        responseString = "ok";
                        break;
                    case HttpStatusCode.NotAcceptable:
                        responseString = await response.Content.ReadAsStringAsync();
                        var content = JsonConvert.DeserializeObject<dynamic>(responseString);
                        responseString = content.url;
                        break;
                    case HttpStatusCode.Unauthorized:
                        responseString = "unauthorized";
                        break;
                    default:
                        responseString = $"Error calling API. StatusCode=${response.StatusCode}";
                        break;
                }
            }
            catch (MsalUiRequiredException ex)
            {
                responseString = $"Session has expired. Please sign in again. {ex.Message}";
            }
            catch (Exception ex)
            {
                responseString = $"Error calling API: {ex.Message}";
            }

            return responseString;
        }
Clone this wiki locally