forked from hermanho/MMM-GooglePhotos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_token_v2.js
46 lines (43 loc) · 1.27 KB
/
generate_token_v2.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
35
36
37
38
39
40
41
42
43
44
45
46
"use strict";
const fs = require("fs");
const path = require("path");
const { mkdirp } = require("mkdirp");
const { authenticate } = require("@google-cloud/local-auth");
const config = require("./google_auth.json");
/**
* @param {string} keyFilePath a path of the GCP credential keyfile
*/
function testKeyFile(keyFilePath) {
if (!fs.existsSync(keyFilePath)) {
throw new Error(`keyfile ${keyFilePath} does not exists`);
}
try {
const keyFile = JSON.parse(fs.readFileSync(keyFilePath, "utf8"));
const keys = keyFile.installed || keyFile.web;
if (!keys) {
throw new Error();
}
} catch (err) {
throw new Error(`keyfile ${keyFilePath} is not a valid GCP credential keyfile`);
}
}
/**
*
*/
async function generate() {
const keyFilePath = path.resolve(__dirname, config.keyFilePath);
testKeyFile(keyFilePath);
const client = await authenticate({
keyfilePath: keyFilePath,
scopes: [config.scope],
});
if (client.credentials && config.savedTokensPath) {
if (config.savedTokensPath) {
const tp = path.resolve(__dirname, config.savedTokensPath);
await mkdirp(path.dirname(tp));
fs.writeFileSync(tp, JSON.stringify(client.credentials));
console.log("Token is generated. check it. (ls -al)");
}
}
}
generate();