This commit is contained in:
nin0 2025-04-25 04:59:33 -04:00
parent 55054e7c9e
commit 182f12f32c
Signed by: nin0
SSH key fingerprint: SHA256:NOoDnFVvZNFvqfXCIhzr6oCTDImZAbTTuyAysZ8Ufk8
4 changed files with 22 additions and 18 deletions

View file

@ -10,7 +10,7 @@
"tsx": "^4.19.3"
},
"scripts": {
"dev": "tsx --watch --env-file-if-exists=.env index.ts",
"dev": "tsx --watch --env-file-if-exists=.env src/index.ts",
"build": "node build.mjs",
"start": "node build.mjs && node dist/index.js"
}

0
src/SpotifyAPI.ts Normal file
View file

View file

@ -1,7 +1,7 @@
import Fastify from "fastify";
import { Yapper } from "./Yapper";
import path from "path";
import { readFileSync, writeFileSync } from "fs";
import { readFileSync, stat, writeFileSync } from "fs";
const state = new Proxy(
JSON.parse(readFileSync(path.join(__dirname, "state.json"), "utf-8")),
@ -53,24 +53,28 @@ fastify.get("/auth", async (request, reply) => {
fastify.get("/callback", async (request, reply) => {
if ((request.query as any).error) return reply.code(400).send();
const tokenRes = await fetch("https://accounts.spotify.com/api/token", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Authorization:
"Basic " +
Buffer.from(
process.env.CLIENT_ID + ":" + process.env.CLIENT_SECRET
).toString("base64")
},
body: new URLSearchParams({
code: (request.query as any).code,
redirect_uri: `https://${request.hostname}/callback`,
grant_type: "authorization_code"
const tokenRes = await (
await fetch("https://accounts.spotify.com/api/token", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Authorization:
"Basic " +
Buffer.from(
process.env.CLIENT_ID + ":" + process.env.CLIENT_SECRET
).toString("base64")
},
body: new URLSearchParams({
code: (request.query as any).code,
redirect_uri: `https://${request.hostname}/callback`,
grant_type: "authorization_code"
})
})
});
).json();
console.log(await tokenRes.json());
state.accessToken = tokenRes.access_token;
state.explodesAt = Date.now() + tokenRes.expires_in * 1000;
state.refreshToken = tokenRes.refresh_token;
});
try {