@@ -40,18 +40,14 @@ body: |-
4040 * Instead of specifying the type of client you'd like to use (JWT, OAuth2, etc)
4141 * this library will automatically choose the right client based on the environment.
4242 */
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+ // The modern `fetch` and classic `request` APIs are available
49+ const res = await auth.fetch(url);
50+ console.log(res.data);
5551 ```
5652
5753 ## OAuth2
@@ -81,10 +77,11 @@ body: |-
8177 */
8278 async function main() {
8379 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.
80+ // Make a simple request to the People API using our pre-authenticated client. The `fetch` and
81+ // `request` methods accept a [`GaxiosOptions`](https://github.com/googleapis/gaxios)
82+ // object.
8683 const url = 'https://people.googleapis.com/v1/people/me?personFields=names';
87- const res = await oAuth2Client.request({ url} );
84+ const res = await oAuth2Client.fetch( url);
8885 console.log(res.data);
8986
9087 // After acquiring an access_token, you may want to check on the audience, expiration,
@@ -156,6 +153,7 @@ body: |-
156153 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:
157154
158155 ```js
156+ const auth = new GoogleAuth();
159157 const client = await auth.getClient();
160158
161159 client.on('tokens', (tokens) => {
@@ -166,9 +164,10 @@ body: |-
166164 console.log(tokens.access_token);
167165 });
168166
167+ const projectId = await auth.getProjectId();
169168 const url = `https://dns.googleapis.com/dns/v1/projects/${projectId}`;
170- const res = await client.request({ url });
171169 // The `tokens` event would now be raised if this was the first request
170+ const res = await client.fetch(url);
172171 ```
173172
174173 #### Retrieve access token
@@ -241,18 +240,14 @@ body: |-
241240 const {JWT} = require('google-auth-library');
242241 const keys = require('./jwt.keys.json');
243242
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);
243+ const client = new JWT({
244+ email: keys.client_email,
245+ key: keys.private_key,
246+ scopes: ['https://www.googleapis.com/auth/cloud-platform'],
247+ });
248+ const url = `https://dns.googleapis.com/dns/v1/projects/${keys.project_id}`;
249+ const res = await client.fetch(url);
250+ console.log(res.data);
256251 ```
257252
258253 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 +283,12 @@ body: |-
288283 }
289284 const keys = JSON.parse(keysEnvVar);
290285
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);
286+ // load the JWT or UserRefreshClient from the keys
287+ const client = auth.fromJSON(keys);
288+ client.scopes = ['https://www.googleapis.com/auth/cloud-platform'];
289+ const url = `https://dns.googleapis.com/dns/v1/projects/${keys.project_id}`;
290+ const res = await client.fetch(url);
291+ console.log(res.data);
301292 ```
302293
303294 **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 +304,14 @@ body: |-
313304 ``` js
314305 const {auth, Compute} = require('google-auth-library');
315306
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);
307+ const client = new Compute({
308+ // Specifying the service account email is optional.
309+ serviceAccountEmail: '[email protected] ' 310+ });
311+ const projectId = await auth.getProjectId();
312+ const url = `https://dns.googleapis.com/dns/v1/projects/${projectId}`;
313+ const res = await client.fetch(url);
314+ console.log(res.data);
328315 ```
329316
330317 ## Workload Identity Federation
@@ -1023,17 +1010,14 @@ body: |-
10231010 The library can now automatically choose the right type of client and initialize credentials from the context provided in the configuration file.
10241011
10251012 ```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- }
1013+ const auth = new GoogleAuth({
1014+ scopes: 'https://www.googleapis.com/auth/cloud-platform'
1015+ });
1016+ const projectId = await auth.getProjectId();
1017+ // List all buckets in a project.
1018+ const url = `https://storage.googleapis.com/storage/v1/b?project=${projectId}`;
1019+ const res = await client.fetch(url);
1020+ console.log(res.data);
10371021 ```
10381022
10391023 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 +1040,12 @@ body: |-
10561040 const {ExternalAccountClient} = require('google-auth-library');
10571041 const jsonConfig = require('/path/to/config.json');
10581042
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- }
1043+ const client = ExternalAccountClient.fromJSON(jsonConfig);
1044+ client.scopes = ['https://www.googleapis.com/auth/cloud-platform'];
1045+ // List all buckets in a project.
1046+ const url = `https://storage.googleapis.com/storage/v1/b?project=${projectId}`;
1047+ const res = await client.fetch(url);
1048+ console.log(res.data);
10671049 ```
10681050
10691051 #### Security Considerations
@@ -1087,15 +1069,11 @@ body: |-
10871069 // Make a request to a protected Cloud Run service.
10881070 const {GoogleAuth} = require('google-auth-library');
10891071
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);
1072+ const url = 'https://cloud-run-1234-uc.a.run.app';
1073+ const auth = new GoogleAuth();
1074+ const client = await auth.getIdTokenClient(url);
1075+ const res = await client.fetch(url);
1076+ console.log(res.data);
10991077 ```
11001078
11011079 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 +1085,12 @@ body: |-
11071085 // Make a request to a protected Cloud Identity-Aware Proxy (IAP) resource
11081086 const {GoogleAuth} = require('google-auth-library');
11091087
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);
1088+ const targetAudience = 'iap-client-id';
1089+ const url = 'https://iap-url.com';
1090+ const auth = new GoogleAuth();
1091+ const client = await auth.getIdTokenClient(targetAudience);
1092+ const res = await client.fetch(url);
1093+ console.log(res.data);
11201094 ```
11211095
11221096 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 +1163,7 @@ body: |-
11891163
11901164 // Use impersonated credentials:
11911165 const url = 'https://www.googleapis.com/storage/v1/b?project=anotherProjectID'
1192- const resp = await targetClient.request({ url } );
1166+ const resp = await targetClient.fetch( url);
11931167 for (const bucket of resp.data.items) {
11941168 console.log(bucket.name);
11951169 }
0 commit comments