Common exports
This commit is contained in:
parent
e94a52d00a
commit
8cfcb72f0c
23
src/common.js
Normal file
23
src/common.js
Normal file
@ -0,0 +1,23 @@
|
||||
import fetch from "node-fetch";
|
||||
import {dirname} from "path";
|
||||
import {fileURLToPath} from "url";
|
||||
import {readFileSync} from "fs";
|
||||
|
||||
// Read and parse JSON. There is no __dirname for modules
|
||||
export const secretsFile = `${dirname(fileURLToPath(import.meta.url))}/../secrets.json`;
|
||||
export const secrets = JSON.parse(readFileSync(secretsFile, "utf8"));
|
||||
|
||||
export async function getToken() {
|
||||
const response = await fetch("https://oauth2.googleapis.com/token", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
client_id: secrets.clientId,
|
||||
client_secret: secrets.clientSecret,
|
||||
grant_type: "refresh_token",
|
||||
refresh_token: secrets.refreshToken
|
||||
}),
|
||||
headers: {'Content-Type': 'application/json'}
|
||||
});
|
||||
const data = await response.json();
|
||||
return data.access_token;
|
||||
}
|
@ -1,34 +1,14 @@
|
||||
import fetch from "node-fetch";
|
||||
import readline from "readline";
|
||||
import {readFileSync, writeFileSync} from "fs";
|
||||
import { exit } from "process";
|
||||
import {dirname} from "path";
|
||||
import {fileURLToPath} from "url";
|
||||
|
||||
// Read and parse JSON. There is no __dirname for modules
|
||||
const secretsFile = `${dirname(fileURLToPath(import.meta.url))}/../secrets.json`;
|
||||
const secrets = JSON.parse(readFileSync(secretsFile, "utf8"));
|
||||
import { writeFileSync } from "fs";
|
||||
import { secretsFile, secrets, getToken } from "./common.js";
|
||||
|
||||
if (!secrets.refreshToken) {
|
||||
console.error("No refresh token found. Please run 'npm run getToken' first.");
|
||||
exit();
|
||||
}
|
||||
|
||||
async function getToken() {
|
||||
const response = await fetch("https://oauth2.googleapis.com/token", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
client_id: secrets.clientId,
|
||||
client_secret: secrets.clientSecret,
|
||||
grant_type: "refresh_token",
|
||||
refresh_token: secrets.refreshToken
|
||||
}),
|
||||
headers: {'Content-Type': 'application/json'}
|
||||
});
|
||||
const data = await response.json();
|
||||
return data.access_token;
|
||||
}
|
||||
|
||||
const rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
|
@ -2,9 +2,8 @@ import express from "express";
|
||||
import bodyParser from "body-parser";
|
||||
import open from "open";
|
||||
import fetch from "node-fetch";
|
||||
import {readFileSync, writeFileSync} from "fs";
|
||||
import {dirname} from "path";
|
||||
import {fileURLToPath} from "url";
|
||||
import { writeFileSync } from "fs";
|
||||
import { secretsFile, secrets } from "./common.js";
|
||||
|
||||
const app = express()
|
||||
app.use(bodyParser.urlencoded({ extended: true }));
|
||||
@ -13,10 +12,6 @@ const port = 8080;
|
||||
|
||||
let state = "code";
|
||||
|
||||
// Read and parse JSON. There is no __dirname for modules
|
||||
const secretsFile = `${dirname(fileURLToPath(import.meta.url))}/../secrets.json`;
|
||||
const secrets = JSON.parse(readFileSync(secretsFile, "utf8"));
|
||||
|
||||
// Express route that prints request body and query parameters
|
||||
app.use("/", (req, res) => {
|
||||
switch (state) {
|
||||
|
Loading…
Reference in New Issue
Block a user