OLD | NEW |
(Empty) | |
| 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; |
| 18 using System.Diagnostics; |
| 19 using System.Threading; |
| 20 using System.Threading.Tasks; |
| 21 |
| 22 using Google.Apis.Auth.OAuth2.Requests; |
| 23 using Google.Apis.Auth.OAuth2.Responses; |
| 24 using Google.Apis.Logging; |
| 25 |
| 26 namespace Google.Apis.Auth.OAuth2 |
| 27 { |
| 28 /// <summary>OAuth 2.0 verification code receiver that reads the authorizati
on code from the user input.</summary> |
| 29 public class PromptCodeReceiver : ICodeReceiver |
| 30 { |
| 31 private static readonly ILogger Logger = ApplicationContext.Logger.ForTy
pe<PromptCodeReceiver>(); |
| 32 |
| 33 public string RedirectUri |
| 34 { |
| 35 get { return GoogleAuthConsts.InstalledAppRedirectUri; } |
| 36 } |
| 37 |
| 38 public Task<AuthorizationCodeResponseUrl> ReceiveCodeAsync(Authorization
CodeRequestUrl url, CancellationToken taskCancellationToken) |
| 39 { |
| 40 var tcs = new TaskCompletionSource<AuthorizationCodeResponseUrl>(); |
| 41 |
| 42 var authorizationUrl = url.Build().ToString(); |
| 43 |
| 44 Logger.Debug("Open a browser with \"{0}\" URL", authorizationUrl); |
| 45 Process.Start(authorizationUrl); |
| 46 |
| 47 string code = string.Empty; |
| 48 while (string.IsNullOrEmpty(code)) |
| 49 { |
| 50 Console.WriteLine("Please enter code: "); |
| 51 code = Console.ReadLine(); |
| 52 } |
| 53 |
| 54 Logger.Debug("Code is: \"{0}\"", code); |
| 55 |
| 56 tcs.SetResult(new AuthorizationCodeResponseUrl |
| 57 { |
| 58 Code = code |
| 59 }); |
| 60 return tcs.Task; |
| 61 } |
| 62 } |
| 63 } |
OLD | NEW |