public async Task <Token> Authenticate(CloudCredentials credentials)
        {
            if (credentials == null)
            {
                throw new ArgumentNullException("credentials");
            }

            using (var httpClientHandler = new AcceptHeaderOverrideHttpClientHandler())
            {
                if (this.httpProxy != null)
                {
                    httpClientHandler.Proxy = new SimpleProxy(this.httpProxy);
                }

                httpClientHandler.OverrideAcceptHeader      = "application/json";
                httpClientHandler.SkipCertificateValidation = this.skipCertificateValidation;

                var client = new OAuth2Client(this.oauthTarget, this.oauthClient, this.oauthSecret, httpClientHandler);

                this.token.Expires = DateTime.Now;

                var tokenResponse = await client.RequestResourceOwnerPasswordAsync(credentials.User, credentials.Password);

                CheckTokenResponseError(tokenResponse);

                this.token.AccessToken  = tokenResponse.AccessToken;
                this.token.RefreshToken = tokenResponse.RefreshToken;
                this.token.Expires      = this.token.Expires.AddSeconds(tokenResponse.ExpiresIn);
            }

            return(this.token);
        }
        private async Task <TokenResponse> RefreshToken(string refreshToken)
        {
            using (var httpClientHandler = new AcceptHeaderOverrideHttpClientHandler())
            {
                if (this.httpProxy != null)
                {
                    httpClientHandler.Proxy = new SimpleProxy(this.httpProxy);
                }

                httpClientHandler.OverrideAcceptHeader      = "application/json";
                httpClientHandler.SkipCertificateValidation = this.skipCertificateValidation;

                var client        = new OAuth2Client(this.oauthTarget, this.oauthClient, this.oauthSecret, httpClientHandler);
                var tokenResponse = await client.RequestRefreshTokenAsync(refreshToken);

                CheckTokenResponseError(tokenResponse);
                return(tokenResponse);
            }
        }