LEFT | RIGHT |
(no file at all) | |
| 1 /* |
| 2 Copyright 2013 Google Inc |
| 3 |
| 4 Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 you may not use this file except in compliance with the License. |
| 6 You may obtain a copy of the License at |
| 7 |
| 8 http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
| 10 Unless required by applicable law or agreed to in writing, software |
| 11 distributed under the License is distributed on an "AS IS" BASIS, |
| 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 See the License for the specific language governing permissions and |
| 14 limitations under the License. |
| 15 */ |
| 16 |
| 17 using System.Net.Http; |
| 18 using System.Threading; |
| 19 using System.Threading.Tasks; |
| 20 |
| 21 using Google.Apis.Auth.OAuth2.Requests; |
| 22 using Google.Apis.Auth.OAuth2.Responses; |
| 23 using Google.Apis.Json; |
| 24 using Google.Apis.Requests.Parameters; |
| 25 using Google.Apis.Util; |
| 26 |
| 27 namespace Google.Apis.Auth.OAuth2 |
| 28 { |
| 29 /// <summary> |
| 30 /// Extension methods to <seealso cref="Google.Apis.Auth.OAuth2.Requests.Toe
knRequest"/>. |
| 31 /// </summary> |
| 32 internal static class TokenRequestExtenstions |
| 33 { |
| 34 /// <summary> |
| 35 /// Executes the token request in order to receive a· |
| 36 /// <seealso cref="Google.Apis.Auth.OAuth2.Responses.TokenResponse"/>. I
n case the token server returns an· |
| 37 /// error, a <seealso cref="Google.Apis.Auth.OAuth2.Responses.TokenRespo
nseException"/> is thrown. |
| 38 /// </summary> |
| 39 /// <param name="request">The token request.</param> |
| 40 /// <param name="httpClient">The HTTP client used to create an HTTP requ
est.</param> |
| 41 /// <param name="tokenServerUrl">The token server URL.</param> |
| 42 /// <param name="taskCancellationToken">Cancellation token to cancel ope
ration.</param> |
| 43 /// <param name="clock">The clock which is used to set the <seealso cref
="Clock.Issued"/> property.</param> |
| 44 /// <returns>Token response with the new access token.</returns> |
| 45 public static async Task<TokenResponse> Execute(this TokenRequest reques
t, HttpClient httpClient, |
| 46 string tokenServerUrl, CancellationToken taskCancellationToken, IClo
ck clock) |
| 47 { |
| 48 var httpRequest = new HttpRequestMessage(HttpMethod.Post, tokenServe
rUrl); |
| 49 httpRequest.Content = ParameterUtils.CreateFormUrlEncodedContent(req
uest); |
| 50 |
| 51 var response = await httpClient.SendAsync(httpRequest, taskCancellat
ionToken).ConfigureAwait(false); |
| 52 var content = await response.Content.ReadAsStringAsync().ConfigureAw
ait(false); |
| 53 |
| 54 if (!response.IsSuccessStatusCode) |
| 55 { |
| 56 var error = NewtonsoftJsonSerializer.Instance.Deserialize<TokenE
rrorResponse>(content); |
| 57 throw new TokenResponseException(error); |
| 58 } |
| 59 |
| 60 // Gets the token and sets its issued time. |
| 61 var newToken = NewtonsoftJsonSerializer.Instance.Deserialize<TokenRe
sponse>(content); |
| 62 newToken.Issued = clock.Now; |
| 63 return newToken; |
| 64 } |
| 65 } |
| 66 } |
LEFT | RIGHT |