LEFT | RIGHT |
1 /* | 1 /* |
2 Copyright 2013 Google Inc | 2 Copyright 2013 Google Inc |
3 | 3 |
4 Licensed under the Apache License, Version 2.0 (the "License"); | 4 Licensed under the Apache License, Version 2.0 (the "License"); |
5 you may not use this file except in compliance with 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 | 6 You may obtain a copy of the License at |
7 | 7 |
8 http://www.apache.org/licenses/LICENSE-2.0 | 8 http://www.apache.org/licenses/LICENSE-2.0 |
9 | 9 |
10 Unless required by applicable law or agreed to in writing, software | 10 Unless required by applicable law or agreed to in writing, software |
(...skipping 18 matching lines...) Expand all Loading... |
29 /// <summary> | 29 /// <summary> |
30 /// Extension methods to <seealso cref="Google.Apis.Auth.OAuth2.Requests.Toe
knRequest"/>. | 30 /// Extension methods to <seealso cref="Google.Apis.Auth.OAuth2.Requests.Toe
knRequest"/>. |
31 /// </summary> | 31 /// </summary> |
32 internal static class TokenRequestExtenstions | 32 internal static class TokenRequestExtenstions |
33 { | 33 { |
34 /// <summary> | 34 /// <summary> |
35 /// Executes the token request in order to receive a· | 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· | 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. | 37 /// error, a <seealso cref="Google.Apis.Auth.OAuth2.Responses.TokenRespo
nseException"/> is thrown. |
38 /// </summary> | 38 /// </summary> |
39 /// <param name="request">The token request</param> | 39 /// <param name="request">The token request.</param> |
40 /// <param name="httpClient">The HTTP client used to create an HTTP requ
est</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> | 41 /// <param name="tokenServerUrl">The token server URL.</param> |
42 /// <param name="taskCancellationToken">Cancellation token to cancel ope
ration</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> | 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> | 44 /// <returns>Token response with the new access token.</returns> |
45 public static async Task<TokenResponse> Execute(this TokenRequest reques
t, HttpClient httpClient, | 45 public static async Task<TokenResponse> Execute(this TokenRequest reques
t, HttpClient httpClient, |
46 string tokenServerUrl, CancellationToken taskCancellationToken, IClo
ck clock) | 46 string tokenServerUrl, CancellationToken taskCancellationToken, IClo
ck clock) |
47 { | 47 { |
48 var httpRequest = new HttpRequestMessage(HttpMethod.Post, tokenServe
rUrl); | 48 var httpRequest = new HttpRequestMessage(HttpMethod.Post, tokenServe
rUrl); |
49 httpRequest.Content = ParameterUtils.CreateFormUrlEncodedContent(req
uest); | 49 httpRequest.Content = ParameterUtils.CreateFormUrlEncodedContent(req
uest); |
50 | 50 |
51 var response = await httpClient.SendAsync(httpRequest, taskCancellat
ionToken).ConfigureAwait(false); | 51 var response = await httpClient.SendAsync(httpRequest, taskCancellat
ionToken).ConfigureAwait(false); |
52 var content = await response.Content.ReadAsStringAsync().ConfigureAw
ait(false); | 52 var content = await response.Content.ReadAsStringAsync().ConfigureAw
ait(false); |
53 | 53 |
54 if (!response.IsSuccessStatusCode) | 54 if (!response.IsSuccessStatusCode) |
55 { | 55 { |
56 var error = NewtonsoftJsonSerializer.Instance.Deserialize<TokenE
rrorResponse>(content); | 56 var error = NewtonsoftJsonSerializer.Instance.Deserialize<TokenE
rrorResponse>(content); |
57 throw new TokenResponseException(error); | 57 throw new TokenResponseException(error); |
58 } | 58 } |
59 | 59 |
60 // Gets the token and sets its issued time. | 60 // Gets the token and sets its issued time. |
61 var newToken = NewtonsoftJsonSerializer.Instance.Deserialize<TokenRe
sponse>(content); | 61 var newToken = NewtonsoftJsonSerializer.Instance.Deserialize<TokenRe
sponse>(content); |
62 newToken.Issued = clock.Now; | 62 newToken.Issued = clock.Now; |
63 return newToken; | 63 return newToken; |
64 } | 64 } |
65 } | 65 } |
66 } | 66 } |
LEFT | RIGHT |