Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 /* | 1 /* |
2 Copyright 2014 Google Inc | 2 Copyright 2014 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 30 matching lines...) Expand all Loading... | |
41 /// <param name="user">The user to authorize.</param> | 41 /// <param name="user">The user to authorize.</param> |
42 /// <param name="taskCancellationToken">Cancellation token to cancel an operation.</param> | 42 /// <param name="taskCancellationToken">Cancellation token to cancel an operation.</param> |
43 /// <returns>User credential.</returns> | 43 /// <returns>User credential.</returns> |
44 public static async Task<UserCredential> AuthorizeAsync(Uri clientSecret sUri, IEnumerable<string> scopes, | 44 public static async Task<UserCredential> AuthorizeAsync(Uri clientSecret sUri, IEnumerable<string> scopes, |
45 string user, CancellationToken taskCancellationToken) | 45 string user, CancellationToken taskCancellationToken) |
46 { | 46 { |
47 var clientSecrets = await LoadClientSecrets(clientSecretsUri).Config ureAwait(false); | 47 var clientSecrets = await LoadClientSecrets(clientSecretsUri).Config ureAwait(false); |
48 return await AuthorizeAsync(clientSecrets, scopes, user, taskCancell ationToken).ConfigureAwait(false); | 48 return await AuthorizeAsync(clientSecrets, scopes, user, taskCancell ationToken).ConfigureAwait(false); |
49 } | 49 } |
50 | 50 |
51 /// <summary> | |
52 /// Asynchronously reauthorizes the user. This method should be called i f the users want to authorize after· | |
53 /// they revoked the token. | |
54 /// </summary> | |
55 /// <param name="userCredential">The current user credential. Its <see c ref="UserCredential.Token"/> will be | |
56 /// updated. </param> | |
57 /// <param name="taskCancellationToken">Cancellation token to cancel an operation.</param> | |
58 public static async Task ReauthorizeAsync(UserCredential userCredential, | |
59 CancellationToken taskCancellationToken) | |
60 { | |
61 var installedApp = new AuthorizationCodeWPInstalledApp(userCredentia l.Flow); | |
62 // Create an authorization code installed app instance and authorize the user. | |
63 UserCredential newUserCredential = await installedApp.AuthorizeAsync ( | |
64 userCredential.UserId, taskCancellationToken).ConfigureAwait(fal se); | |
65 userCredential.Token = newUserCredential.Token; | |
66 } | |
67 | |
51 /// <summary>Asynchronously authorizes the specified user.</summary> | 68 /// <summary>Asynchronously authorizes the specified user.</summary> |
52 /// <remarks> | 69 /// <remarks> |
53 /// It uses <see cref="Google.Apis.Util.Store.PasswordVaultDataStore"/> as the flow's data store by default. | 70 /// It uses <see cref="Google.Apis.Util.Store.PasswordVaultDataStore"/> as the flow's data store by default. |
54 /// </remarks> | 71 /// </remarks> |
55 /// <param name="clientSecrets">The client secrets URI.</param> | 72 /// <param name="clientSecrets">The client secrets URI.</param> |
56 /// <param name="scopes"> | 73 /// <param name="scopes"> |
57 /// The scopes which indicate the Google API access your application is requesting. | 74 /// The scopes which indicate the Google API access your application is requesting. |
58 /// </param> | 75 /// </param> |
59 /// <param name="user">The user to authorize.</param> | 76 /// <param name="user">The user to authorize.</param> |
60 /// <param name="taskCancellationToken">Cancellation token to cancel an operation.</param> | 77 /// <param name="taskCancellationToken">Cancellation token to cancel an operation.</param> |
61 /// <returns>User credential.</returns> | 78 /// <returns>User credential.</returns> |
62 private static async Task<UserCredential> AuthorizeAsync(ClientSecrets c lientSecrets, | 79 private static async Task<UserCredential> AuthorizeAsync(ClientSecrets c lientSecrets, |
63 IEnumerable<string> scopes, string user, CancellationToken taskCance llationToken) | 80 IEnumerable<string> scopes, string user, CancellationToken taskCance llationToken) |
64 { | 81 { |
65 var initializer = new GoogleAuthorizationCodeFlow.Initializer | 82 var initializer = new GoogleAuthorizationCodeFlow.Initializer |
66 { | 83 { |
67 ClientSecrets = clientSecrets, | 84 ClientSecrets = clientSecrets, |
68 Scopes = scopes, | 85 Scopes = scopes, |
69 DataStore = new PasswordVaultDataStore() | 86 DataStore = new PasswordVaultDataStore() |
70 }; | 87 }; |
71 | 88 |
72 var installedApp = new AuthorizationCodeWPInstalledApp(new GoogleAut horizationCodeFlow(initializer)); | 89 var installedApp = new AuthorizationCodeWPInstalledApp(new GoogleAut horizationCodeFlow(initializer)); |
73 return await installedApp.AuthorizeAsync(user, taskCancellationToken ).ConfigureAwait(false); | 90 return await installedApp.AuthorizeAsync(user, taskCancellationToken ).ConfigureAwait(false); |
74 } | 91 } |
75 | 92 |
76 /// <summary> | |
peleyal
2014/11/25 20:15:55
change the order, public methods should appear fir
peleyal
2014/11/26 02:54:00
Done.
| |
77 /// Asynchronously reauthorizes the user. This method should be called i f the users want to authorize after· | |
78 /// they revoked the token. | |
79 /// </summary> | |
80 /// <param name="userCredential">The current user credential. Its <see c ref="UserCredential.Token"/> will be | |
81 /// updated. </param> | |
82 /// <param name="taskCancellationToken">Cancellation token to cancel an operation.</param> | |
83 public static async Task ReauthorizeAsync(UserCredential userCredential, | |
84 CancellationToken taskCancellationToken) | |
85 { | |
86 var installedApp = new AuthorizationCodeWPInstalledApp(userCredentia l.Flow); | |
87 // Create an authorization code installed app instance and authorize the user. | |
88 UserCredential newUserCredential = await installedApp.AuthorizeAsync ( | |
89 userCredential.UserId, taskCancellationToken).ConfigureAwait(fal se); | |
90 userCredential.Token = newUserCredential.Token; | |
91 } | |
92 | |
93 /// <summary>Loads the client secrets from the given URI.</summary> | 93 /// <summary>Loads the client secrets from the given URI.</summary> |
94 /// <param name="clientSecretsUri">The client secrets URI.</param> | 94 /// <param name="clientSecretsUri">The client secrets URI.</param> |
95 /// <returns>Client secrets.</returns> | 95 /// <returns>Client secrets.</returns> |
96 private static async Task<ClientSecrets> LoadClientSecrets(Uri clientSec retsUri) | 96 private static async Task<ClientSecrets> LoadClientSecrets(Uri clientSec retsUri) |
97 { | 97 { |
98 var file = await StorageFile.GetFileFromApplicationUriAsync(clientSe cretsUri); | 98 var file = await StorageFile.GetFileFromApplicationUriAsync(clientSe cretsUri); |
99 var content = await FileIO.ReadTextAsync(file); | 99 var content = await FileIO.ReadTextAsync(file); |
100 | 100 |
101 using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(content) )) | 101 using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(content) )) |
102 { | 102 { |
103 return GoogleClientSecrets.Load(stream).Secrets; | 103 return GoogleClientSecrets.Load(stream).Secrets; |
104 } | 104 } |
105 } | 105 } |
106 } | 106 } |
107 } | 107 } |
LEFT | RIGHT |