internal async Task <TokenResponse> FetchTokenAsync(string userId, TokenRequest request,
                                                            CancellationToken taskCancellationToken)
        {
            // Add client id and client secret to requests.
            request.ClientId     = ClientSecrets.ClientId;
            request.ClientSecret = ClientSecrets.ClientSecret;

            TokenResponseException tokenException = null;

            try
            {
                var tokenResponse = await request.ExecuteAsync
                                        (httpClient, TokenServerUrl, taskCancellationToken, Clock).ConfigureAwait(false);

                return(tokenResponse);
            }
            catch (TokenResponseException ex)
            {
                // In case there is an exception during getting the token, we delete any user's token information from
                // the data store.
                tokenException = ex;
            }
            await DeleteTokenAsync(userId, taskCancellationToken).ConfigureAwait(false);

            throw tokenException;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates an authentication exception from an <see cref="AggregateException"/>.
        /// </summary>
        /// <param name="e">The aggregate exception from the server.</param>
        /// <param name="message">The error message.</param>
        /// <returns>The Ads OAuth exception for rethrowing.</returns>
        private AdsOAuthException CreateOAuthException(AggregateException e, string message)
        {
            TokenResponseException innerException = e.InnerExceptions.FirstOrDefault()
                                                    as TokenResponseException;

            if (innerException != null)
            {
                return(new AdsOAuthException(message, e, innerException.Error, innerException.StatusCode));
            }
            else
            {
                return(new AdsOAuthException($"{message} See inner exception for details.", e));
            }
        }