Skip to content

Instantly share code, notes, and snippets.

@manoj-choudhari-git

manoj-choudhari-git/Program.cs Secret

Created May 26, 2020
Embed
What would you like to do?
Program.cs file for Daemon App
class Program
{
static async Task Main(string[] args)
{
try
{
var result = await DaemonAppBootstrapper.BootstrapAsync(ConfigurationManager.Current.Api.Scopes);
await ProcessAsync(ConfigurationManager.Current.Api.BaseUrl, result.AccessToken);
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Daemon App Failed : {0}", ex);
Console.ResetColor();
}
}
private static async Task ProcessAsync(string webApiUrl, string accessToken)
{
var client = new HttpClient();
if (!string.IsNullOrEmpty(accessToken))
{
var defaultRequetHeaders = client.DefaultRequestHeaders;
if (defaultRequetHeaders.Accept == null || !defaultRequetHeaders.Accept.Any(m => m.MediaType == "application/json"))
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
defaultRequetHeaders.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
HttpResponseMessage response = await client.GetAsync(webApiUrl);
if (response.IsSuccessStatusCode)
{
string json = await response.Content.ReadAsStringAsync();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("================================================");
Console.WriteLine("API Output:");
Console.WriteLine(json);
Console.WriteLine("================================================");
Console.ResetColor();
return;
}
}
throw new Exception("API Call Failed");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment