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 fetch from "node-fetch";
|
||||||
import readline from "readline";
|
import readline from "readline";
|
||||||
import {readFileSync, writeFileSync} from "fs";
|
|
||||||
import { exit } from "process";
|
import { exit } from "process";
|
||||||
import {dirname} from "path";
|
import { writeFileSync } from "fs";
|
||||||
import {fileURLToPath} from "url";
|
import { secretsFile, secrets, getToken } from "./common.js";
|
||||||
|
|
||||||
// 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"));
|
|
||||||
|
|
||||||
if (!secrets.refreshToken) {
|
if (!secrets.refreshToken) {
|
||||||
console.error("No refresh token found. Please run 'npm run getToken' first.");
|
console.error("No refresh token found. Please run 'npm run getToken' first.");
|
||||||
exit();
|
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({
|
const rl = readline.createInterface({
|
||||||
input: process.stdin,
|
input: process.stdin,
|
||||||
output: process.stdout
|
output: process.stdout
|
||||||
|
@ -2,9 +2,8 @@ import express from "express";
|
|||||||
import bodyParser from "body-parser";
|
import bodyParser from "body-parser";
|
||||||
import open from "open";
|
import open from "open";
|
||||||
import fetch from "node-fetch";
|
import fetch from "node-fetch";
|
||||||
import {readFileSync, writeFileSync} from "fs";
|
import { writeFileSync } from "fs";
|
||||||
import {dirname} from "path";
|
import { secretsFile, secrets } from "./common.js";
|
||||||
import {fileURLToPath} from "url";
|
|
||||||
|
|
||||||
const app = express()
|
const app = express()
|
||||||
app.use(bodyParser.urlencoded({ extended: true }));
|
app.use(bodyParser.urlencoded({ extended: true }));
|
||||||
@ -13,10 +12,6 @@ const port = 8080;
|
|||||||
|
|
||||||
let state = "code";
|
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
|
// Express route that prints request body and query parameters
|
||||||
app.use("/", (req, res) => {
|
app.use("/", (req, res) => {
|
||||||
switch (state) {
|
switch (state) {
|
||||||
|
Loading…
Reference in New Issue
Block a user