-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·34 lines (27 loc) · 1.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env node
const { GoogleAuth } = require('google-auth-library');
const assert = require('assert');
const fs = require('fs');
// input: environment variable (process.env) GOOGLE_APPLICATION_CREDENTIALS
// stdout output: token for that (service) client
// from https://github.com/googleapis/google-auth-library-nodejs
/**
* Instead of specifying the type of client you'd like to use (JWT, OAuth2, etc)
* this library will automatically choose the right client based on the environment.
*/
async function main() {
// validate assumptions about GOOGLE_APPLICATION_CREDENTIALS env var (as assumed by google-auth-library.GoogleAuth implementation...)
const GOOGLE_APPLICATION_CREDENTIALS = process.env.GOOGLE_APPLICATION_CREDENTIALS
assert(GOOGLE_APPLICATION_CREDENTIALS, 'GOOGLE_APPLICATION_CREDENTIALS environment variable is not set!');
assert(fs.existsSync(GOOGLE_APPLICATION_CREDENTIALS), `GOOGLE_APPLICATION_CREDENTIALS file '${GOOGLE_APPLICATION_CREDENTIALS}' does not exist!`);
console.error(`fetching token for GOOGLE_APPLICATION_CREDENTIALS="${GOOGLE_APPLICATION_CREDENTIALS}"...`);
const auth = new GoogleAuth({ scopes: 'https://www.googleapis.com/auth/cloud-platform' });
const token = await auth.getAccessToken();
assert(token.length > 0, 'retrieved token has 0 length?!')
// stdout == token
console.log(token);
}
main().catch(error => {
console.error({ error });
process.exit(1);
});