Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(339)

Unified Diff: README.md

Issue 1308163004: Async-ify. (Closed) Base URL: git@github.com:dart-lang/oauth2.git@master
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | lib/src/authorization_code_grant.dart » ('j') | lib/src/authorization_code_grant.dart » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: README.md
diff --git a/README.md b/README.md
index 52b724c9d5bf6125bf4bf2bf69a1f2414392dbd4..23be5ff2047324bf4312934f7b6612152f3996b5 100644
--- a/README.md
+++ b/README.md
@@ -48,53 +48,60 @@ final secret = "my client secret";
// query parameters.
final redirectUrl = Uri.parse("http://my-site.com/oauth2-redirect");
-var credentialsFile = new File("~/.myapp/credentials.json");
-return credentialsFile.exists().then((exists) {
- // If the OAuth2 credentials have already been saved from a previous
- // run, we just want to reload them.
+/// A file in which the users credentials are stored persistently. If the server
+/// issues a refresh token allowing the client to refresh outdated credentials,
+/// these may be valid indefinitely, meaning the user never has to
+/// re-authenticate.
+final credentialsFile = new File("~/.myapp/credentials.json");
+
+/// Either load an OAuth2 client from saved credentials or authenticate a new
+/// one.
+Future<oauth2.Client> getClient() async {
+ var exists = await credentialsFile.exists();
+
+ // If the OAuth2 credentials have already been saved from a previous run, we
+ // just want to reload them.
if (exists) {
- return credentialsFile.readAsString().then((json) {
- var credentials = new oauth2.Credentials.fromJson(json);
- return new oauth2.Client(identifier, secret, credentials);
- });
+ var credentials = new oauth2.Credentials.fromJson(
+ await credentialsFile.readAsString());
+ return new oauth2.Client(identifier, secret, credentials);
}
- // If we don't have OAuth2 credentials yet, we need to get the
- // resource owner to authorize us. We're assuming here that we're a
- // command-line application.
+ // If we don't have OAuth2 credentials yet, we need to get the resource owner
+ // to authorize us. We're assuming here that we're a command-line application.
var grant = new oauth2.AuthorizationCodeGrant(
identifier, secret, authorizationEndpoint, tokenEndpoint);
- // Redirect the resource owner to the authorization URL. This will be
- // a URL on the authorization server (authorizationEndpoint with some
- // additional query parameters). Once the resource owner has
- // authorized, they'll be redirected to `redirectUrl` with an
- // authorization code.
+ // Redirect the resource owner to the authorization URL. This will be a URL on
+ // the authorization server (authorizationEndpoint with some additional query
+ // parameters). Once the resource owner has authorized, they'll be redirected
+ // to `redirectUrl` with an authorization code.
//
// `redirect` is an imaginary function that redirects the resource
// owner's browser.
- return redirect(grant.getAuthorizationUrl(redirectUrl)).then((_) {
- // Another imaginary function that listens for a request to
- // `redirectUrl`.
- return listen(redirectUrl);
- }).then((request) {
- // Once the user is redirected to `redirectUrl`, pass the query
- // parameters to the AuthorizationCodeGrant. It will validate them
- // and extract the authorization code to create a new Client.
- return grant.handleAuthorizationResponse(request.uri.queryParameters);
- })
-}).then((client) {
- // Once you have a Client, you can use it just like any other HTTP
- // client.
- return client.read("http://example.com/protected-resources.txt")
- .then((result) {
- // Once we're done with the client, save the credentials file. This
- // ensures that if the credentials were automatically refreshed
- // while using the client, the new credentials are available for the
- // next run of the program.
- return credentialsFile.open(FileMode.WRITE).then((file) {
- return file.writeString(client.credentials.toJson());
- }).then((file) => file.close()).then((_) => result);
- });
-}).then(print);
+ await redirect(grant.getAuthorizationUrl(redirectUrl));
+
+ // Another imaginary function that listens for a request to `redirectUrl`.
+ var request = await listen(redirectUrl);
+
+ // Once the user is redirected to `redirectUrl`, pass the query parameters to
+ // the AuthorizationCodeGrant. It will validate them and extract the
+ // authorization code to create a new Client.
+ return await grant.handleAuthorizationResponse(request.uri.queryParameters);
+}
+
+main() async {
+ var client = await loadClient();
+
+ // Once you have a Client, you can use it just like any other HTTP client.
+ var result = client.read("http://example.com/protected-resources.txt");
+
+ // Once we're done with the client, save the credentials file. This ensures
+ // that if the credentials were automatically refreshed while using the
+ // client, the new credentials are available for the next run of the
+ // program.
+ await credentialsFile.writeAsString(client.credentials.toJson());
+
+ print(result);
+}
```
« no previous file with comments | « no previous file | lib/src/authorization_code_grant.dart » ('j') | lib/src/authorization_code_grant.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698