OLD | NEW |
1 A client library for authenticating with a remote service via OAuth2 on | 1 A client library for authenticating with a remote service via OAuth2 on |
2 behalf of a user, and making authorized HTTP requests with the user's OAuth2 | 2 behalf of a user, and making authorized HTTP requests with the user's OAuth2 |
3 credentials. | 3 credentials. |
4 | 4 |
5 OAuth2 allows a client (the program using this library) to access and | 5 OAuth2 allows a client (the program using this library) to access and |
6 manipulate a resource that's owned by a resource owner (the end user) and | 6 manipulate a resource that's owned by a resource owner (the end user) and |
7 lives on a remote server. The client directs the resource owner to an | 7 lives on a remote server. The client directs the resource owner to an |
8 authorization server (usually but not always the same as the server that | 8 authorization server (usually but not always the same as the server that |
9 hosts the resource), where the resource owner tells the authorization server | 9 hosts the resource), where the resource owner tells the authorization server |
10 to give the client an access token. This token serves as proof that the | 10 to give the client an access token. This token serves as proof that the |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 /// Either load an OAuth2 client from saved credentials or authenticate a new | 57 /// Either load an OAuth2 client from saved credentials or authenticate a new |
58 /// one. | 58 /// one. |
59 Future<oauth2.Client> getClient() async { | 59 Future<oauth2.Client> getClient() async { |
60 var exists = await credentialsFile.exists(); | 60 var exists = await credentialsFile.exists(); |
61 | 61 |
62 // If the OAuth2 credentials have already been saved from a previous run, we | 62 // If the OAuth2 credentials have already been saved from a previous run, we |
63 // just want to reload them. | 63 // just want to reload them. |
64 if (exists) { | 64 if (exists) { |
65 var credentials = new oauth2.Credentials.fromJson( | 65 var credentials = new oauth2.Credentials.fromJson( |
66 await credentialsFile.readAsString()); | 66 await credentialsFile.readAsString()); |
67 return new oauth2.Client(identifier, secret, credentials); | 67 return new oauth2.Client(credentials, |
| 68 identifier: identifier, secret: secret); |
68 } | 69 } |
69 | 70 |
70 // If we don't have OAuth2 credentials yet, we need to get the resource owner | 71 // If we don't have OAuth2 credentials yet, we need to get the resource owner |
71 // to authorize us. We're assuming here that we're a command-line application. | 72 // to authorize us. We're assuming here that we're a command-line application. |
72 var grant = new oauth2.AuthorizationCodeGrant( | 73 var grant = new oauth2.AuthorizationCodeGrant( |
73 identifier, secret, authorizationEndpoint, tokenEndpoint); | 74 identifier, authorizationEndpoint, tokenEndpoint, |
| 75 secret: secret); |
74 | 76 |
75 // Redirect the resource owner to the authorization URL. This will be a URL on | 77 // Redirect the resource owner to the authorization URL. This will be a URL on |
76 // the authorization server (authorizationEndpoint with some additional query | 78 // the authorization server (authorizationEndpoint with some additional query |
77 // parameters). Once the resource owner has authorized, they'll be redirected | 79 // parameters). Once the resource owner has authorized, they'll be redirected |
78 // to `redirectUrl` with an authorization code. | 80 // to `redirectUrl` with an authorization code. |
79 // | 81 // |
80 // `redirect` is an imaginary function that redirects the resource | 82 // `redirect` is an imaginary function that redirects the resource |
81 // owner's browser. | 83 // owner's browser. |
82 await redirect(grant.getAuthorizationUrl(redirectUrl)); | 84 await redirect(grant.getAuthorizationUrl(redirectUrl)); |
83 | 85 |
(...skipping 14 matching lines...) Expand all Loading... |
98 | 100 |
99 // Once we're done with the client, save the credentials file. This ensures | 101 // Once we're done with the client, save the credentials file. This ensures |
100 // that if the credentials were automatically refreshed while using the | 102 // that if the credentials were automatically refreshed while using the |
101 // client, the new credentials are available for the next run of the | 103 // client, the new credentials are available for the next run of the |
102 // program. | 104 // program. |
103 await credentialsFile.writeAsString(client.credentials.toJson()); | 105 await credentialsFile.writeAsString(client.credentials.toJson()); |
104 | 106 |
105 print(result); | 107 print(result); |
106 } | 108 } |
107 ``` | 109 ``` |
OLD | NEW |