Added script to fetch refresh token
This commit is contained in:
parent
41c22ec04d
commit
0fc999a019
80
getToken.js
Normal file
80
getToken.js
Normal file
@ -0,0 +1,80 @@
|
||||
import express from "express";
|
||||
import bodyParser from "body-parser";
|
||||
import open from "open";
|
||||
import fetch from "node-fetch";
|
||||
import {readFileSync, writeFileSync} from "fs"
|
||||
|
||||
const app = express()
|
||||
app.use(bodyParser.urlencoded({ extended: true }));
|
||||
app.use(bodyParser.json());
|
||||
const port = 8080;
|
||||
|
||||
let state = "code";
|
||||
|
||||
// Read and parse JSON
|
||||
const secrets = JSON.parse(readFileSync("./secrets.json", "utf8"));
|
||||
|
||||
// Express route that prints request body and query parameters
|
||||
app.use("/", (req, res) => {
|
||||
switch (state) {
|
||||
case "code": {
|
||||
state = "token";
|
||||
getToken(req.query);
|
||||
res.status(200).json(req.query).end();
|
||||
break;
|
||||
}
|
||||
case "token": {
|
||||
console.log(req.body);
|
||||
res.status(200).json(req.body).end();
|
||||
break;
|
||||
}
|
||||
default: res.status(400).end();
|
||||
}
|
||||
});
|
||||
|
||||
app.use("/token", (req, res) => {
|
||||
console.log({
|
||||
get: req.query,
|
||||
post: req.body
|
||||
});
|
||||
res.status(200).json({
|
||||
get: req.query,
|
||||
post: req.body
|
||||
}).end()
|
||||
});
|
||||
|
||||
const server = app.listen(port, () => console.log(`Example app listening on port ${port}!`));
|
||||
|
||||
const token = "miau";
|
||||
|
||||
const params = new URLSearchParams({
|
||||
scope: "https://www.googleapis.com/auth/photoslibrary.readonly",
|
||||
response_type: "code",
|
||||
state: token,
|
||||
redirect_uri: "http://localhost:8080",
|
||||
client_id: secrets.clientId
|
||||
});
|
||||
|
||||
open(`https://accounts.google.com/o/oauth2/v2/auth?${params.toString()}`);
|
||||
|
||||
async function getToken(googleResponse) {
|
||||
const response = await fetch("https://oauth2.googleapis.com/token", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
client_id: secrets.clientId,
|
||||
client_secret: secrets.clientSecret,
|
||||
code: googleResponse.code,
|
||||
grant_type: "authorization_code",
|
||||
redirect_uri: "http://localhost:8080"
|
||||
}),
|
||||
headers: {'Content-Type': 'application/json'}
|
||||
});
|
||||
const data = await response.json();
|
||||
console.log("Got the token!");
|
||||
console.log("Writing token to secrets...");
|
||||
secrets.refreshToken = data.refresh_token;
|
||||
writeFileSync("./secrets.json", JSON.stringify(secrets));
|
||||
console.log("Done!");
|
||||
|
||||
server.close();
|
||||
}
|
@ -5,7 +5,7 @@
|
||||
"main": "index.js",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"server": "node index.js",
|
||||
"token": "node getToken.js",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [
|
||||
|
Loading…
Reference in New Issue
Block a user