Skip to content

Commit 25cb2ef

Browse files
1 parent e3e87da commit 25cb2ef

File tree

1 file changed

+62
-89
lines changed

1 file changed

+62
-89
lines changed

‎README.md

+62-89
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,13 @@ const {GoogleAuth} = require('google-auth-library');
8484
* Instead of specifying the type of client you'd like to use (JWT, OAuth2, etc)
8585
* this library will automatically choose the right client based on the environment.
8686
*/
87-
async function main() {
88-
const auth = new GoogleAuth({
89-
scopes: 'https://www.googleapis.com/auth/cloud-platform'
90-
});
91-
const client = await auth.getClient();
92-
const projectId = await auth.getProjectId();
93-
const url = `https://dns.googleapis.com/dns/v1/projects/${projectId}`;
94-
const res = await client.request({ url });
95-
console.log(res.data);
96-
}
97-
98-
main().catch(console.error);
87+
const auth = new GoogleAuth({
88+
scopes: 'https://www.googleapis.com/auth/cloud-platform'
89+
});
90+
const projectId = await auth.getProjectId();
91+
const url = `https://dns.googleapis.com/dns/v1/projects/${projectId}`;
92+
const res = await auth.fetch(url);
93+
console.log(res.data);
9994
```
10095

10196
## OAuth2
@@ -125,10 +120,11 @@ const keys = require('./oauth2.keys.json');
125120
*/
126121
async function main() {
127122
const oAuth2Client = await getAuthenticatedClient();
128-
// Make a simple request to the People API using our pre-authenticated client. The `request()` method
129-
// takes an GaxiosOptions object. Visit https://github.com/JustinBeckwith/gaxios.
123+
// Make a simple request to the People API using our pre-authenticated client. The `fetch` and
124+
// `request` methods accept a [`GaxiosOptions`](https://github.com/googleapis/gaxios)
125+
// object.
130126
const url = 'https://people.googleapis.com/v1/people/me?personFields=names';
131-
const res = await oAuth2Client.request({url});
127+
const res = await oAuth2Client.fetch(url);
132128
console.log(res.data);
133129

134130
// After acquiring an access_token, you may want to check on the audience, expiration,
@@ -200,6 +196,7 @@ main().catch(console.error);
200196
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:
201197

202198
```js
199+
const auth = new GoogleAuth();
203200
const client = await auth.getClient();
204201

205202
client.on('tokens', (tokens) => {
@@ -210,9 +207,10 @@ client.on('tokens', (tokens) => {
210207
console.log(tokens.access_token);
211208
});
212209

210+
const projectId = await auth.getProjectId();
213211
const url = `https://dns.googleapis.com/dns/v1/projects/${projectId}`;
214-
const res = await client.request({ url });
215212
// The `tokens` event would now be raised if this was the first request
213+
const res = await client.fetch(url);
216214
```
217215

218216
#### Retrieve access token
@@ -285,18 +283,14 @@ The Google Developers Console provides a `.json` file that you can use to config
285283
const {JWT} = require('google-auth-library');
286284
const keys = require('./jwt.keys.json');
287285

288-
async function main() {
289-
const client = new JWT({
290-
email: keys.client_email,
291-
key: keys.private_key,
292-
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
293-
});
294-
const url = `https://dns.googleapis.com/dns/v1/projects/${keys.project_id}`;
295-
const res = await client.request({url});
296-
console.log(res.data);
297-
}
298-
299-
main().catch(console.error);
286+
const client = new JWT({
287+
email: keys.client_email,
288+
key: keys.private_key,
289+
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
290+
});
291+
const url = `https://dns.googleapis.com/dns/v1/projects/${keys.project_id}`;
292+
const res = await client.fetch(url);
293+
console.log(res.data);
300294
```
301295

302296
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).
@@ -332,16 +326,12 @@ if (!keysEnvVar) {
332326
}
333327
const keys = JSON.parse(keysEnvVar);
334328

335-
async function main() {
336-
// load the JWT or UserRefreshClient from the keys
337-
const client = auth.fromJSON(keys);
338-
client.scopes = ['https://www.googleapis.com/auth/cloud-platform'];
339-
const url = `https://dns.googleapis.com/dns/v1/projects/${keys.project_id}`;
340-
const res = await client.request({url});
341-
console.log(res.data);
342-
}
343-
344-
main().catch(console.error);
329+
// load the JWT or UserRefreshClient from the keys
330+
const client = auth.fromJSON(keys);
331+
client.scopes = ['https://www.googleapis.com/auth/cloud-platform'];
332+
const url = `https://dns.googleapis.com/dns/v1/projects/${keys.project_id}`;
333+
const res = await client.fetch(url);
334+
console.log(res.data);
345335
```
346336

347337
**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).
@@ -357,18 +347,14 @@ If your application is running on Google Cloud Platform, you can authenticate us
357347
``` js
358348
const {auth, Compute} = require('google-auth-library');
359349

360-
async function main() {
361-
const client = new Compute({
362-
// Specifying the service account email is optional.
363-
serviceAccountEmail: '[email protected]'
364-
});
365-
const projectId = await auth.getProjectId();
366-
const url = `https://dns.googleapis.com/dns/v1/projects/${projectId}`;
367-
const res = await client.request({url});
368-
console.log(res.data);
369-
}
370-
371-
main().catch(console.error);
350+
const client = new Compute({
351+
// Specifying the service account email is optional.
352+
serviceAccountEmail: '[email protected]'
353+
});
354+
const projectId = await auth.getProjectId();
355+
const url = `https://dns.googleapis.com/dns/v1/projects/${projectId}`;
356+
const res = await client.fetch(url);
357+
console.log(res.data);
372358
```
373359

374360
## Workload Identity Federation
@@ -1067,17 +1053,14 @@ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/config.json
10671053
The library can now automatically choose the right type of client and initialize credentials from the context provided in the configuration file.
10681054

10691055
```js
1070-
async function main() {
1071-
const auth = new GoogleAuth({
1072-
scopes: 'https://www.googleapis.com/auth/cloud-platform'
1073-
});
1074-
const client = await auth.getClient();
1075-
const projectId = await auth.getProjectId();
1076-
// List all buckets in a project.
1077-
const url = `https://storage.googleapis.com/storage/v1/b?project=${projectId}`;
1078-
const res = await client.request({ url });
1079-
console.log(res.data);
1080-
}
1056+
const auth = new GoogleAuth({
1057+
scopes: 'https://www.googleapis.com/auth/cloud-platform'
1058+
});
1059+
const projectId = await auth.getProjectId();
1060+
// List all buckets in a project.
1061+
const url = `https://storage.googleapis.com/storage/v1/b?project=${projectId}`;
1062+
const res = await client.fetch(url);
1063+
console.log(res.data);
10811064
```
10821065

10831066
When using external identities with Application Default Credentials in Node.js, the `roles/browser` role needs to be granted to the service account.
@@ -1100,14 +1083,12 @@ You can also explicitly initialize external account clients using the generated
11001083
const {ExternalAccountClient} = require('google-auth-library');
11011084
const jsonConfig = require('/path/to/config.json');
11021085

1103-
async function main() {
1104-
const client = ExternalAccountClient.fromJSON(jsonConfig);
1105-
client.scopes = ['https://www.googleapis.com/auth/cloud-platform'];
1106-
// List all buckets in a project.
1107-
const url = `https://storage.googleapis.com/storage/v1/b?project=${projectId}`;
1108-
const res = await client.request({url});
1109-
console.log(res.data);
1110-
}
1086+
const client = ExternalAccountClient.fromJSON(jsonConfig);
1087+
client.scopes = ['https://www.googleapis.com/auth/cloud-platform'];
1088+
// List all buckets in a project.
1089+
const url = `https://storage.googleapis.com/storage/v1/b?project=${projectId}`;
1090+
const res = await client.fetch(url);
1091+
console.log(res.data);
11111092
```
11121093

11131094
#### Security Considerations
@@ -1131,15 +1112,11 @@ IAM permission.
11311112
// Make a request to a protected Cloud Run service.
11321113
const {GoogleAuth} = require('google-auth-library');
11331114

1134-
async function main() {
1135-
const url = 'https://cloud-run-1234-uc.a.run.app';
1136-
const auth = new GoogleAuth();
1137-
const client = await auth.getIdTokenClient(url);
1138-
const res = await client.request({url});
1139-
console.log(res.data);
1140-
}
1141-
1142-
main().catch(console.error);
1115+
const url = 'https://cloud-run-1234-uc.a.run.app';
1116+
const auth = new GoogleAuth();
1117+
const client = await auth.getIdTokenClient(url);
1118+
const res = await client.fetch(url);
1119+
console.log(res.data);
11431120
```
11441121

11451122
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).
@@ -1151,16 +1128,12 @@ used when you set up your protected resource as the target audience.
11511128
// Make a request to a protected Cloud Identity-Aware Proxy (IAP) resource
11521129
const {GoogleAuth} = require('google-auth-library');
11531130

1154-
async function main()
1155-
const targetAudience = 'iap-client-id';
1156-
const url = 'https://iap-url.com';
1157-
const auth = new GoogleAuth();
1158-
const client = await auth.getIdTokenClient(targetAudience);
1159-
const res = await client.request({url});
1160-
console.log(res.data);
1161-
}
1162-
1163-
main().catch(console.error);
1131+
const targetAudience = 'iap-client-id';
1132+
const url = 'https://iap-url.com';
1133+
const auth = new GoogleAuth();
1134+
const client = await auth.getIdTokenClient(targetAudience);
1135+
const res = await client.fetch(url);
1136+
console.log(res.data);
11641137
```
11651138

11661139
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).
@@ -1233,7 +1206,7 @@ async function main() {
12331206

12341207
// Use impersonated credentials:
12351208
const url = 'https://www.googleapis.com/storage/v1/b?project=anotherProjectID'
1236-
const resp = await targetClient.request({ url });
1209+
const resp = await targetClient.fetch(url);
12371210
for (const bucket of resp.data.items) {
12381211
console.log(bucket.name);
12391212
}

0 commit comments

Comments
 (0)