25 lines
1.1 KiB
JavaScript
25 lines
1.1 KiB
JavaScript
import fs from "node:fs";
|
|
import readline from "node:readline";
|
|
import path from "node:path";
|
|
import { google } from "googleapis";
|
|
import { config } from "../src/config.js";
|
|
|
|
const credentials = JSON.parse(fs.readFileSync(config.credentialsPath, "utf8"));
|
|
const client = credentials.installed || credentials.web;
|
|
if (!client) throw new Error("Expected an installed or web OAuth client");
|
|
const oauth = new google.auth.OAuth2(client.client_id, client.client_secret, client.redirect_uris?.[0]);
|
|
const url = oauth.generateAuthUrl({
|
|
access_type: "offline",
|
|
prompt: "consent",
|
|
scope: ["https://www.googleapis.com/auth/drive.file"],
|
|
});
|
|
console.log(`Open this URL and approve access:\n\n${url}\n`);
|
|
const terminal = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
terminal.question("Paste the returned authorization code: ", async (code) => {
|
|
terminal.close();
|
|
const { tokens } = await oauth.getToken(code.trim());
|
|
fs.mkdirSync(path.dirname(config.tokenPath), { recursive: true });
|
|
fs.writeFileSync(config.tokenPath, JSON.stringify(tokens, null, 2));
|
|
console.log(`Google token saved to ${config.tokenPath}`);
|
|
});
|