Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(2061)

Unified Diff: Src/GoogleApis.Auth/OAuth2/TokenRequestExtenstions.cs

Issue 13972043: Issue 351: Reimplement OAuth2 (Step 3 - Tests, Flows and Credential) (Closed) Base URL: https://google-api-dotnet-client.googlecode.com/hg/
Patch Set: minor Created 10 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Src/GoogleApis.Auth/OAuth2/Responses/TokenResponse.cs ('k') | Src/GoogleApis.Auth/OAuth2/UserCredential.cs » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Src/GoogleApis.Auth/OAuth2/TokenRequestExtenstions.cs
===================================================================
new file mode 100644
--- /dev/null
b/Src/GoogleApis.Auth/OAuth2/TokenRequestExtenstions.cs
@@ -0,0 1,66 @@
/*
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.Requests;
using Google.Apis.Auth.OAuth2.Responses;
using Google.Apis.Json;
using Google.Apis.Requests.Parameters;
using Google.Apis.Util;
namespace Google.Apis.Auth.OAuth2
{
/// <summary>
/// Extension methods to <seealso cref="Google.Apis.Auth.OAuth2.Requests.ToeknRequest"/>.
/// </summary>
internal static class TokenRequestExtenstions
{
/// <summary>
/// Executes the token request in order to receive a
/// <seealso cref="Google.Apis.Auth.OAuth2.Responses.TokenResponse"/>. In case the token server returns an
/// error, a <seealso 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 <seealso cref="Clock.Issued"/> property.</param>
/// <returns>Token response with the new access token.</returns>
public static async Task<TokenResponse> Execute(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;
}
}
}
« no previous file with comments | « Src/GoogleApis.Auth/OAuth2/Responses/TokenResponse.cs ('k') | Src/GoogleApis.Auth/OAuth2/UserCredential.cs » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b