public 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;
        }
예제 #2
0
 /// <summary>
 /// Executes the token request in order to receive a
 /// <see cref="Google.Apis.Auth.OAuth2.Responses.TokenResponse"/>. In case the token server returns an
 /// error, a <see cref="Google.Apis.Auth.OAuth2.Responses.TokenResponseException"/> is thrown.
 /// </summary>
 /// <param name="request">The token request.</param>
 /// <param name="httpClient">The HTTP client used to create an HTTP request.</param>
 /// <param name="tokenServerUrl">The token server URL.</param>
 /// <param name="taskCancellationToken">Cancellation token to cancel operation.</param>
 /// <param name="clock">
 /// The clock which is used to set the
 /// <see cref="Google.Apis.Auth.OAuth2.Responses.TokenResponse.Issued"/> property.
 /// </param>
 /// <returns>Token response with the new access token.</returns>
 public static Task <TokenResponse> ExecuteAsync(this TokenRequest request, HttpClient httpClient,
                                                 string tokenServerUrl, CancellationToken taskCancellationToken, IClock clock) =>
 ExecuteAsync(request, httpClient, tokenServerUrl, taskCancellationToken, clock, ApplicationContext.Logger);