@@ -40,18 +40,13 @@ body: |-
40
40
* Instead of specifying the type of client you'd like to use (JWT, OAuth2, etc)
41
41
* this library will automatically choose the right client based on the environment.
42
42
*/
43
- async function main() {
44
- const auth = new GoogleAuth({
45
- scopes: 'https://www.googleapis.com/auth/cloud-platform'
46
- });
47
- const client = await auth.getClient();
48
- const projectId = await auth.getProjectId();
49
- const url = `https://dns.googleapis.com/dns/v1/projects/${projectId}`;
50
- const res = await client.request({ url });
51
- console.log(res.data);
52
- }
53
-
54
- main().catch(console.error);
43
+ const auth = new GoogleAuth({
44
+ scopes: 'https://www.googleapis.com/auth/cloud-platform'
45
+ });
46
+ const projectId = await auth.getProjectId();
47
+ const url = `https://dns.googleapis.com/dns/v1/projects/${projectId}`;
48
+ const res = await auth.fetch(url);
49
+ console.log(res.data);
55
50
```
56
51
57
52
## OAuth2
@@ -81,10 +76,11 @@ body: |-
81
76
*/
82
77
async function main() {
83
78
const oAuth2Client = await getAuthenticatedClient();
84
- // Make a simple request to the People API using our pre-authenticated client. The `request()` method
85
- // takes an GaxiosOptions object. Visit https://github.com/JustinBeckwith/gaxios.
79
+ // Make a simple request to the People API using our pre-authenticated client. The `fetch` and
80
+ // `request` methods accept a [`GaxiosOptions`](https://github.com/googleapis/gaxios)
81
+ // object.
86
82
const url = 'https://people.googleapis.com/v1/people/me?personFields=names';
87
- const res = await oAuth2Client.request({ url} );
83
+ const res = await oAuth2Client.fetch( url);
88
84
console.log(res.data);
89
85
90
86
// After acquiring an access_token, you may want to check on the audience, expiration,
@@ -156,6 +152,7 @@ body: |-
156
152
This library will automatically obtain an `access_token`, and automatically refresh the `access_token` if a `refresh_token` is present. The `refresh_token` is only returned on the [first authorization](https://github.com/googleapis/google-api-nodejs-client/issues/750#issuecomment-304521450), so if you want to make sure you store it safely. An easy way to make sure you always store the most recent tokens is to use the `tokens` event:
157
153
158
154
```js
155
+ const auth = new GoogleAuth();
159
156
const client = await auth.getClient();
160
157
161
158
client.on('tokens', (tokens) => {
@@ -166,9 +163,10 @@ body: |-
166
163
console.log(tokens.access_token);
167
164
});
168
165
166
+ const projectId = await auth.getProjectId();
169
167
const url = `https://dns.googleapis.com/dns/v1/projects/${projectId}`;
170
- const res = await client.request({ url });
171
168
// The `tokens` event would now be raised if this was the first request
169
+ const res = await client.fetch(url);
172
170
```
173
171
174
172
#### Retrieve access token
@@ -241,18 +239,14 @@ body: |-
241
239
const {JWT} = require('google-auth-library');
242
240
const keys = require('./jwt.keys.json');
243
241
244
- async function main() {
245
- const client = new JWT({
246
- email: keys.client_email,
247
- key: keys.private_key,
248
- scopes: ['https://www.googleapis.com/auth/cloud-platform'],
249
- });
250
- const url = `https://dns.googleapis.com/dns/v1/projects/${keys.project_id}`;
251
- const res = await client.request({url});
252
- console.log(res.data);
253
- }
254
-
255
- main().catch(console.error);
242
+ const client = new JWT({
243
+ email: keys.client_email,
244
+ key: keys.private_key,
245
+ scopes: ['https://www.googleapis.com/auth/cloud-platform'],
246
+ });
247
+ const url = `https://dns.googleapis.com/dns/v1/projects/${keys.project_id}`;
248
+ const res = await client.fetch(url);
249
+ console.log(res.data);
256
250
```
257
251
258
252
The parameters for the JWT auth client including how to use it with a `.pem` file are explained in [samples/jwt.js](https://github.com/googleapis/google-auth-library-nodejs/blob/main/samples/jwt.js).
@@ -288,16 +282,12 @@ body: |-
288
282
}
289
283
const keys = JSON.parse(keysEnvVar);
290
284
291
- async function main() {
292
- // load the JWT or UserRefreshClient from the keys
293
- const client = auth.fromJSON(keys);
294
- client.scopes = ['https://www.googleapis.com/auth/cloud-platform'];
295
- const url = `https://dns.googleapis.com/dns/v1/projects/${keys.project_id}`;
296
- const res = await client.request({url});
297
- console.log(res.data);
298
- }
299
-
300
- main().catch(console.error);
285
+ // load the JWT or UserRefreshClient from the keys
286
+ const client = auth.fromJSON(keys);
287
+ client.scopes = ['https://www.googleapis.com/auth/cloud-platform'];
288
+ const url = `https://dns.googleapis.com/dns/v1/projects/${keys.project_id}`;
289
+ const res = await client.fetch(url);
290
+ console.log(res.data);
301
291
```
302
292
303
293
**Important**: If you accept a credential configuration (credential JSON/File/Stream) from an external source for authentication to Google Cloud, you must validate it before providing it to any Google API or library. Providing an unvalidated credential configuration to Google APIs can compromise the security of your systems and data. For more information, refer to [Validate credential configurations from external sources](https://cloud.google.com/docs/authentication/external/externally-sourced-credentials).
@@ -313,18 +303,14 @@ body: |-
313
303
``` js
314
304
const {auth, Compute} = require('google-auth-library');
315
305
316
- async function main() {
317
- const client = new Compute({
318
- // Specifying the service account email is optional.
319
- serviceAccountEmail: '[email protected] '
320
- });
321
- const projectId = await auth.getProjectId();
322
- const url = `https://dns.googleapis.com/dns/v1/projects/${projectId}`;
323
- const res = await client.request({url});
324
- console.log(res.data);
325
- }
326
-
327
- main().catch(console.error);
306
+ const client = new Compute({
307
+ // Specifying the service account email is optional.
308
+ serviceAccountEmail: '[email protected] '
309
+ });
310
+ const projectId = await auth.getProjectId();
311
+ const url = `https://dns.googleapis.com/dns/v1/projects/${projectId}`;
312
+ const res = await client.fetch(url);
313
+ console.log(res.data);
328
314
```
329
315
330
316
## Workload Identity Federation
@@ -1023,17 +1009,14 @@ body: |-
1023
1009
The library can now automatically choose the right type of client and initialize credentials from the context provided in the configuration file.
1024
1010
1025
1011
```js
1026
- async function main() {
1027
- const auth = new GoogleAuth({
1028
- scopes: 'https://www.googleapis.com/auth/cloud-platform'
1029
- });
1030
- const client = await auth.getClient();
1031
- const projectId = await auth.getProjectId();
1032
- // List all buckets in a project.
1033
- const url = `https://storage.googleapis.com/storage/v1/b?project=${projectId}`;
1034
- const res = await client.request({ url });
1035
- console.log(res.data);
1036
- }
1012
+ const auth = new GoogleAuth({
1013
+ scopes: 'https://www.googleapis.com/auth/cloud-platform'
1014
+ });
1015
+ const projectId = await auth.getProjectId();
1016
+ // List all buckets in a project.
1017
+ const url = `https://storage.googleapis.com/storage/v1/b?project=${projectId}`;
1018
+ const res = await client.fetch(url);
1019
+ console.log(res.data);
1037
1020
```
1038
1021
1039
1022
When using external identities with Application Default Credentials in Node.js, the `roles/browser` role needs to be granted to the service account.
@@ -1056,14 +1039,12 @@ body: |-
1056
1039
const {ExternalAccountClient} = require('google-auth-library');
1057
1040
const jsonConfig = require('/path/to/config.json');
1058
1041
1059
- async function main() {
1060
- const client = ExternalAccountClient.fromJSON(jsonConfig);
1061
- client.scopes = ['https://www.googleapis.com/auth/cloud-platform'];
1062
- // List all buckets in a project.
1063
- const url = `https://storage.googleapis.com/storage/v1/b?project=${projectId}`;
1064
- const res = await client.request({url});
1065
- console.log(res.data);
1066
- }
1042
+ const client = ExternalAccountClient.fromJSON(jsonConfig);
1043
+ client.scopes = ['https://www.googleapis.com/auth/cloud-platform'];
1044
+ // List all buckets in a project.
1045
+ const url = `https://storage.googleapis.com/storage/v1/b?project=${projectId}`;
1046
+ const res = await client.fetch(url);
1047
+ console.log(res.data);
1067
1048
```
1068
1049
1069
1050
#### Security Considerations
@@ -1087,15 +1068,11 @@ body: |-
1087
1068
// Make a request to a protected Cloud Run service.
1088
1069
const {GoogleAuth} = require('google-auth-library');
1089
1070
1090
- async function main() {
1091
- const url = 'https://cloud-run-1234-uc.a.run.app';
1092
- const auth = new GoogleAuth();
1093
- const client = await auth.getIdTokenClient(url);
1094
- const res = await client.request({url});
1095
- console.log(res.data);
1096
- }
1097
-
1098
- main().catch(console.error);
1071
+ const url = 'https://cloud-run-1234-uc.a.run.app';
1072
+ const auth = new GoogleAuth();
1073
+ const client = await auth.getIdTokenClient(url);
1074
+ const res = await client.fetch(url);
1075
+ console.log(res.data);
1099
1076
```
1100
1077
1101
1078
A complete example can be found in [`samples/idtokens-serverless.js`](https://github.com/googleapis/google-auth-library-nodejs/blob/main/samples/idtokens-serverless.js).
@@ -1107,16 +1084,12 @@ body: |-
1107
1084
// Make a request to a protected Cloud Identity-Aware Proxy (IAP) resource
1108
1085
const {GoogleAuth} = require('google-auth-library');
1109
1086
1110
- async function main()
1111
- const targetAudience = 'iap-client-id';
1112
- const url = 'https://iap-url.com';
1113
- const auth = new GoogleAuth();
1114
- const client = await auth.getIdTokenClient(targetAudience);
1115
- const res = await client.request({url});
1116
- console.log(res.data);
1117
- }
1118
-
1119
- main().catch(console.error);
1087
+ const targetAudience = 'iap-client-id';
1088
+ const url = 'https://iap-url.com';
1089
+ const auth = new GoogleAuth();
1090
+ const client = await auth.getIdTokenClient(targetAudience);
1091
+ const res = await client.fetch(url);
1092
+ console.log(res.data);
1120
1093
```
1121
1094
1122
1095
A complete example can be found in [`samples/idtokens-iap.js`](https://github.com/googleapis/google-auth-library-nodejs/blob/main/samples/idtokens-iap.js).
@@ -1189,7 +1162,7 @@ body: |-
1189
1162
1190
1163
// Use impersonated credentials:
1191
1164
const url = 'https://www.googleapis.com/storage/v1/b?project=anotherProjectID'
1192
- const resp = await targetClient.request({ url } );
1165
+ const resp = await targetClient.fetch( url);
1193
1166
for (const bucket of resp.data.items) {
1194
1167
console.log(bucket.name);
1195
1168
}
0 commit comments