google-oauth-access-token-w.../server.js
imnyang 152f3ec045 callback.html 파일 추가 및 OAuth 콜백 처리 로직 구현
package.json에 node-cron 및 node-fetch 의존성 추가
server.js에서 루트 경로에 대한 리다이렉트 로직 추가
.gitignore 파일 생성
public/index.html 및 callback/callback.html 파일 삭제
2025-07-05 13:11:35 +09:00

52 lines
1.6 KiB
JavaScript

const express = require("express");
const app = express();
const path = require("path");
const fetch = require("node-fetch");
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 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.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({
email: userInfo.email,
name: userInfo.name,
token: token
});
} catch (err) {
console.error("❌ Error:", err);
res.status(500).send("Error");
}
});
app.get("/callback", (req, res) => {
res.sendFile(path.join(__dirname, "callback.html"));
});
const PORT = 39090;
app.listen(PORT, () => {
console.log(`✅ Server running at http://localhost:${PORT}`);
});