h
This commit is contained in:
parent
55054e7c9e
commit
182f12f32c
4 changed files with 22 additions and 18 deletions
|
@ -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
0
src/SpotifyAPI.ts
Normal 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 {
|
Loading…
Add table
Add a link
Reference in a new issue