Exemplo n.º 1
0
        /// <summary>
        /// Exchanges an authorization code for an access token and refresh token
        /// </summary>
        /// <param name="authorizationCode">The authorization code</param>
        /// <returns>An access token, its expiration date, and a refresh token</returns>
        private async Task <(string accessToken, string refreshToken, DateTime expiration)> GetTokens(string authorizationCode)
        {
            string redirectUri  = Properties.Settings.Default.RedirectUri;
            string clientId     = Properties.Settings.Default.ClientId;
            string clientSecret = Properties.Settings.Default.ClientSecret.DecryptString();

            TokenApi tokenApi = new TokenApi()
            {
                AuthorizationHeader = new AuthorizationHeader(clientId, clientSecret)
            };

            dynamic json = await tokenApi.GetAccessCode(
                authorizationCode,
                redirectUri,
                errorCallback : (errorResponse) =>
            {
                MessageBox.Show(
                    errorResponse,
                    "Login failure",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            });

            int      expiresInSeconds = json.expires_in;
            DateTime expiration       = DateTime.UtcNow.AddSeconds(expiresInSeconds);

            return(json.access_token, json.refresh_token, expiration);
        }