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; |
| 18 using System.Collections.Generic; |
| 19 using System.IO; |
| 20 using System.Text; |
| 21 using System.Threading; |
| 22 using System.Threading.Tasks; |
| 23 using Windows.Storage; |
| 24 |
| 25 using Google.Apis.Auth.OAuth2.Flows; |
| 26 using Google.Apis.Util.Store; |
| 27 |
| 28 namespace Google.Apis.Auth.OAuth2 |
| 29 { |
| 30 /// <summary>A helper utility to manage the authorization code flow.</summar
y> |
| 31 public class GoogleWebAuthorizationBroker |
| 32 { |
| 33 /// <summary>Asynchronously authorizes the specified user.</summary> |
| 34 /// <remarks> |
| 35 /// It uses <seealso cref="Google.Apis.Util.Store.StroageDataStore"/> as
the flow's data store by default. |
| 36 /// </remarks> |
| 37 /// <param name="clientSecretsUri">The client secrets URI.</param> |
| 38 /// <param name="scopes"> |
| 39 /// The scopes which indicate the Google API access your application is
requesting. |
| 40 /// </param> |
| 41 /// <param name="user">The user to authorize.</param> |
| 42 /// <param name="taskCancellationToken">Cancellation token to cancel an
operation.</param> |
| 43 /// <returns>User credential.</returns> |
| 44 public static async Task<UserCredential> AuthorizeAsync(Uri clientSecret
sUri, IEnumerable<string> scopes, |
| 45 string user, CancellationToken taskCancellationToken) |
| 46 { |
| 47 var clientSecrets = await LoadClientSecrets(clientSecretsUri); |
| 48 |
| 49 var initializer = new GoogleAuthorizationCodeFlow.Initializer |
| 50 { |
| 51 ClientSecrets = clientSecrets, |
| 52 Scopes = scopes, |
| 53 DataStore = new StroageDataStore() |
| 54 }; |
| 55 |
| 56 var installedApp = new AuthorizationCodeWinRTInstalledApp(initialize
r); |
| 57 return await installedApp.AuthorizeAsync(user, taskCancellationToken
); |
| 58 } |
| 59 |
| 60 /// <summary>Loads the client secret from the given URI.</summary> |
| 61 /// <param name="clientSecretsUri">The client secrets URI.</param> |
| 62 /// <returns>Client secrets.</returns> |
| 63 private static async Task<ClientSecrets> LoadClientSecrets(Uri clientSec
retsUri) |
| 64 { |
| 65 var file = await StorageFile.GetFileFromApplicationUriAsync(clientSe
cretsUri); |
| 66 var content = await FileIO.ReadTextAsync(file); |
| 67 |
| 68 using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(content)
)) |
| 69 { |
| 70 return GoogleClientSecrets.Load(stream).Secrets; |
| 71 } |
| 72 } |
| 73 } |
| 74 } |
LEFT | RIGHT |