/*
Copyright 2013 Google Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Google.Apis.Auth.OAuth2.Responses;
using Google.Apis.Json;
using Google.Apis.Requests.Parameters;
using Google.Apis.Util;
namespace Google.Apis.Auth.OAuth2.Requests
{
/// Extension methods to .
public static class TokenRequestExtenstions
{
///
/// Executes the token request in order to receive a
/// . In case the token server returns an
/// error, a is thrown.
///
/// The token request.
/// The HTTP client used to create an HTTP request.
/// The token server URL.
/// Cancellation token to cancel operation.
///
/// The clock which is used to set the
/// property.
///
/// Token response with the new access token.
public static async Task 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(content);
throw new TokenResponseException(error, response.StatusCode);
}
// Gets the token and sets its issued time.
var newToken = NewtonsoftJsonSerializer.Instance.Deserialize(content);
newToken.IssuedUtc = clock.UtcNow;
return newToken;
}
}
}