public static ProcessStatus ProcessArgs(string[] args, out TokenFetcherConfiguration config) { if (args.Length == 0) { config = null; return(ProcessStatus.NoArguments); } if (args.Length < 2) { config = null; return(ProcessStatus.NotEnoughArguments); } // ReSharper disable once ObjectCreationAsStatement new Url(args[0]); // validate by creatin Url instance. config = new TokenFetcherConfiguration { ServerUrl = args[0], ApiKey = args[1] }; return(ProcessStatus.ValidConfig); }
private static async Task <string> TryObtainAuthToken(TokenFetcherConfiguration config) { var securedAuthenticator = new SecuredAuthenticator(autoRefreshToken: false); var result = await securedAuthenticator.DoOAuthRequestAsync(null, config.ServerUrl + "/OAuth/API-Key", config.ApiKey); using (var httpClient = new HttpClient()) { result(httpClient); var authenticationHeaderValue = httpClient.DefaultRequestHeaders.Authorization; return(authenticationHeaderValue.Parameter); } }
static void Main(string[] args) { TokenFetcherConfiguration config; var parseStatus = TokenFetcherConfiguration.ProcessArgs(args, out config); switch (parseStatus) { case TokenFetcherConfiguration.ProcessStatus.NoArguments: PrintUsage(); break; case TokenFetcherConfiguration.ProcessStatus.NotEnoughArguments: Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Illegal arguments amount, see usage instructions:"); Console.ForegroundColor = ConsoleColor.White; break; case TokenFetcherConfiguration.ProcessStatus.ValidConfig: var token = TryObtainAuthToken(config).Result; Console.WriteLine(token); break; } }