internal static async Task <TokenResponse> ExecuteAsync(this TokenRequest request, HttpClient httpClient, string tokenServerUrl, CancellationToken taskCancellationToken, IClock clock, ILogger logger) { var httpRequest = new HttpRequestMessage(HttpMethod.Post, tokenServerUrl) { Content = ParameterUtils.CreateFormUrlEncodedContent(request) }; var response = await httpClient.SendAsync(httpRequest, taskCancellationToken).ConfigureAwait(false); return(await TokenResponse.FromHttpResponseAsync(response, clock, logger).ConfigureAwait(false)); }
/// <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 async Task <TokenResponse> ExecuteAsync(this TokenRequest request, HttpClient httpClient, string tokenServerUrl, CancellationToken taskCancellationToken, IClock clock) { var httpRequest = new HttpRequestMessage(HttpMethod.Post, tokenServerUrl); httpRequest.Content = ParameterUtils.CreateFormUrlEncodedContent(request); var response = await httpClient.SendAsync(httpRequest, taskCancellationToken).ConfigureAwait(false); var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false); if (!response.IsSuccessStatusCode) { var error = NewtonsoftJsonSerializer.Instance.Deserialize <TokenErrorResponse>(content); throw new TokenResponseException(error); } // Gets the token and sets its issued time. var newToken = NewtonsoftJsonSerializer.Instance.Deserialize <TokenResponse>(content); newToken.Issued = clock.Now; return(newToken); }