Exemple #1
0
        private static async Task MainAsync()
        {
            try
            {
                //Uitschakelen van certificate validation. Niet gebruiken in productie code!
                ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
                TokenCollection tokens = LoadTokens();
                if (!tokens.HasAccessToken)
                {
                    await GetAuthorizationToken(tokens);
                    await GetAccessToken(tokens);

                    SaveTokens(tokens);
                    Console.WriteLine($"Access token & refresh token opgeslagen voor toekomstig gebruik.");
                }
                else if (tokens.IsAccessExpired)
                {
                    await RefreshAccessToken(tokens);

                    SaveTokens(tokens);
                    Console.WriteLine($"Access token & refresh token opgeslagen voor toekomstig gebruik.");
                }
                else
                {
                    Console.WriteLine($"Geldig access token gevonden, deze wordt gebruikt.");
                }
                //nu hebben we een geldig access token
                //test call naar klic api
                Stopwatch sw = new Stopwatch();
                sw.Start();
                for (int i = 0; i < 1; i++)
                {
                    Console.WriteLine($"Verstreken tijd {sw.ElapsedMilliseconds / 1000} sec.");

                    await DoTestCall(tokens.AccessToken, tokens.TestUrl);

                    Thread.Sleep(2000);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Exemple #2
0
        /// <summary>
        /// Haalt een nieuwe Authorisation token op
        /// </summary>
        /// <param name="tokens"></param>
        /// <returns></returns>
        private static async Task GetAuthorizationToken(TokenCollection tokens)
        {
            Console.WriteLine($"Authorisation wordt opgevraagd via browser...");
            // Create an authorization code request.
            var    CodeReceiver          = new LocalServerCodeReceiver();
            string authorizationEndpoint = tokens.AuthorizationEndpoint + "/authorize";

            CodeReceiver.RedirectUri = tokens.RedirectUri;
            string            parameters = $"response_type=code&client_id={tokens.ClientId}&client_secret={tokens.ClientSecret}&redirect_uri={tokens.RedirectUri}&scope={tokens.Scope}";
            CancellationToken taskCancellationToken;
            // Receive the code.
            var response = await CodeReceiver.ReceiveCodeAsync($"{authorizationEndpoint}?{parameters}", taskCancellationToken)
                           .ConfigureAwait(false);

            if (string.IsNullOrWhiteSpace(response))
            {
                throw new ApplicationException($"Authorisation code ophalen is mislukt.");
            }
            Console.WriteLine($"Authorisation code {response} ontvangen");
            tokens.AuthorizationCode = response;
        }
Exemple #3
0
        private static TokenCollection LoadTokens()
        {
            var tokenRepo = new TokenRepository();

            if (!File.Exists(tokenRepo.ConfigFileName))
            {
                //Lege config file aanmaken
                try
                {
                    tokenRepo.SaveTokens(new TokenCollection());
                }
                catch (Exception ex)
                {
                    throw new ApplicationException($"Kan config {tokenRepo.ConfigFileName} niet aanmaken.", ex);
                }
            }
            TokenCollection tokens = tokenRepo.LoadTokens();

            if (!tokens.HasClientId)
            {
                throw new ApplicationException($"Geen ClientId en ClientSecret gevonden. Vraag deze aan bij het kadaster en voeg deze toe aan {tokenRepo.ConfigFileName}");
            }
            return(tokens);
        }
Exemple #4
0
 private static void SaveTokens(TokenCollection tokens)
 {
     new TokenRepository().SaveTokens(tokens);
 }