토큰 수신 엔드포인트를 POST에서 GET으로 변경하고, 응답 형식을 수정하여 사용자 정보를 포함하도록 개선

This commit is contained in:
암냥 2025-07-05 13:59:13 +09:00
commit 8a9ad5fcf2

View file

@ -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;