From 8a9ad5fcf288dd2bd722d93e9734d97739280e01 Mon Sep 17 00:00:00 2001 From: imnyang Date: Sat, 5 Jul 2025 13:59:13 +0900 Subject: [PATCH] =?UTF-8?q?=ED=86=A0=ED=81=B0=20=EC=88=98=EC=8B=A0=20?= =?UTF-8?q?=EC=97=94=EB=93=9C=ED=8F=AC=EC=9D=B8=ED=8A=B8=EB=A5=BC=20POST?= =?UTF-8?q?=EC=97=90=EC=84=9C=20GET=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= =?UTF-8?q?=ED=95=98=EA=B3=A0,=20=EC=9D=91=EB=8B=B5=20=ED=98=95=EC=8B=9D?= =?UTF-8?q?=EC=9D=84=20=EC=88=98=EC=A0=95=ED=95=98=EC=97=AC=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=EC=9E=90=20=EC=A0=95=EB=B3=B4=EB=A5=BC=20=ED=8F=AC?= =?UTF-8?q?=ED=95=A8=ED=95=98=EB=8F=84=EB=A1=9D=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server.js | 70 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 27 deletions(-) diff --git a/server.js b/server.js index 4543f86..56ea121 100644 --- a/server.js +++ b/server.js @@ -6,42 +6,58 @@ const fetch = require("node-fetch"); app.use(express.static("public")); // public 폴더 내 정적 파일 제공 app.use(express.json()); // JSON 본문 파싱 - app.get("/", (req, res) => { - const clientId = "16435018183-9a880bertda0en85387ge8f8mgsves71.apps.googleusercontent.com"; // 반드시 수정 - const redirectUri = "https://google-oauth-access-token-whs.hako.li/callback"; + const clientId = + "16435018183-9a880bertda0en85387ge8f8mgsves71.apps.googleusercontent.com"; // 반드시 수정 + const redirectUri = "https://google-oauth-access-token-whs.hako.li/callback"; - const authUrl = "https://accounts.google.com/o/oauth2/v2/auth?" + - `client_id=${clientId}` + - `&redirect_uri=${redirectUri}` + - `&response_type=token` + - `&scope=email%20profile`; - res.redirect(authUrl); + const authUrl = + "https://accounts.google.com/o/oauth2/v2/auth?" + + `client_id=${clientId}` + + `&redirect_uri=${redirectUri}` + + `&response_type=token` + + `&scope=email%20profile`; + res.redirect(authUrl); }); // Access Token 수신용 엔드포인트 -app.post("/token", async (req, res) => { - const token = req.body.access_token; - try { - const response = await fetch("https://www.googleapis.com/oauth2/v3/userinfo", { - headers: { - Authorization: `Bearer ${token}`, - }, - }); - const userInfo = await response.json(); - console.log("Email:", userInfo.email); - console.log("Name:", userInfo.name); - console.log("Access Token:", token); +app.get("/token", async (req, res) => { + const token = req.query.access_token; + try { + const response = await fetch( + "https://www.googleapis.com/oauth2/v3/userinfo", + { + headers: { + Authorization: `Bearer ${token}`, + }, + } + ); + const userInfo = await response.json(); + console.log("Email:", userInfo.email); + console.log("Name:", userInfo.name); + console.log("Access Token:", token); - res.send("Token received!"); - } catch (err) { - console.error("❌ Error:", err); - res.status(500).send("Error"); - } + res.send({ + success: true, + timestamp: new Date().toISOString(), + user: { + email: userInfo.email, + name: userInfo.name, + picture: userInfo.picture, + }, + tokens: { + accessToken: tokenData.access_token, + scope: tokenData.scope, + }, + }); + } catch (err) { + console.error("❌ Error:", err); + res.status(500).send("Error"); + } }); app.get("/callback", (req, res) => { - res.sendFile(path.join(__dirname, "callback.html")); + res.sendFile(path.join(__dirname, "callback.html")); }); const PORT = 39090;