OLD | NEW |
1 # Package of Google APIs | 1 Auto-generated Dart libraries for accessing [Google APIs][libs]. |
2 | 2 |
3 ## Description | |
4 | |
5 This repository contains auto-generated client libraries for accessing | |
6 Google APIs using dart. It has the usual dart package layout. | |
7 | |
8 ## Usage | 3 ## Usage |
9 | 4 |
10 The first step is to obtain oauth2 access credentials. This can be done using | 5 First, obtain OAuth 2.0 access credentials. This can be done using the |
11 the `googleapis_auth` package. Your application can access APIs on behalf of a | 6 `googleapis_auth` package. Your application can access APIs on behalf of a |
12 user or using a service account. | 7 user or using a service account. |
13 | 8 |
14 After obtaining credentials, an API from the `googleapis` package can be | 9 After obtaining credentials, an API from the `googleapis` package can be |
15 accessed with an authenticated HTTP client. | 10 accessed with an authenticated HTTP client. |
16 | 11 |
17 The following is an example of a command line application which lists files | 12 ## Example |
18 in Google Drive by using a service account. | 13 |
| 14 The following command line application lists files in Google Drive by using a |
| 15 service account. |
19 | 16 |
20 Create a `pubspec.yaml` file with the `googleapis_auth` and `googleapis` | 17 Create a `pubspec.yaml` file with the `googleapis_auth` and `googleapis` |
21 dependencies. | 18 dependencies. |
22 | 19 |
23 ... | 20 ```yaml |
24 dependencies: | 21 ... |
25 googleapis: any | 22 dependencies: |
26 googleapis_auth: any | 23 googleapis: any |
| 24 googleapis_auth: any |
| 25 ``` |
27 | 26 |
28 Create a service account in the Google Cloud Console and save the credential | 27 Create a service account in the Google Cloud Console and save the credential |
29 information. After that the Cloud Storage API can be accessed like this: | 28 information. |
30 | 29 |
31 import 'package:googleapis/storage/v1.dart'; | 30 Then create a Dart application to list files in a spececific project. *In the |
32 import 'package:googleapis_auth/auth_io.dart'; | 31 example below, files from the `dart-on-cloud` project are listed.* |
33 | 32 |
34 final Credentials = new ServiceAccountCredentials.fromJson(r''' | 33 ```dart |
35 { | 34 // bin/list_files.dart |
36 "private_key_id": ..., | |
37 "private_key": ..., | |
38 "client_email": ..., | |
39 "client_id": ..., | |
40 "type": "service_account" | |
41 } | |
42 '''); | |
43 | 35 |
44 void main() { | 36 import 'package:googleapis/storage/v1.dart'; |
45 clientViaServiceAccount(Credentials, | 37 import 'package:googleapis_auth/auth_io.dart'; |
46 [StorageApi.DevstorageReadOnlyScope]).then((http)
{ | 38 |
47 var storage = new StorageApi(http); | 39 final _credentials = new ServiceAccountCredentials.fromJson(r''' |
48 storage.buckets.list('dart-on-cloud').then((buckets) { | 40 { |
49 print("Received ${buckets.items.length} bucket names:"); | 41 "private_key_id": ..., |
50 for (var file in buckets.items) { | 42 "private_key": ..., |
51 print(file.name); | 43 "client_email": ..., |
52 } | 44 "client_id": ..., |
53 }); | 45 "type": "service_account" |
54 }); | 46 } |
55 } | 47 '''); |
| 48 |
| 49 const _SCOPES = const [StorageApi.DevstorageReadOnlyScope]; |
| 50 |
| 51 void main() { |
| 52 clientViaServiceAccount(_credentials, _SCOPES).then((http_client) { |
| 53 var storage = new StorageApi(http_client); |
| 54 storage.buckets.list('dart-on-cloud').then((buckets) { |
| 55 print("Received ${buckets.items.length} bucket names:"); |
| 56 for (var file in buckets.items) { |
| 57 print(file.name); |
| 58 } |
| 59 }); |
| 60 }); |
| 61 } |
| 62 ``` |
| 63 |
| 64 [libs]: https://developers.google.com/discovery/libraries/ |
OLD | NEW |