토큰 수신 엔드포인트를 POST에서 GET으로 변경하고, 응답 형식을 수정하여 사용자 정보를 포함하도록 개선
This commit is contained in:
parent
bc796e806d
commit
8a9ad5fcf2
1 changed files with 43 additions and 27 deletions
32
server.js
32
server.js
|
|
@ -6,12 +6,13 @@ const fetch = require("node-fetch");
|
||||||
app.use(express.static("public")); // public 폴더 내 정적 파일 제공
|
app.use(express.static("public")); // public 폴더 내 정적 파일 제공
|
||||||
app.use(express.json()); // JSON 본문 파싱
|
app.use(express.json()); // JSON 본문 파싱
|
||||||
|
|
||||||
|
|
||||||
app.get("/", (req, res) => {
|
app.get("/", (req, res) => {
|
||||||
const clientId = "16435018183-9a880bertda0en85387ge8f8mgsves71.apps.googleusercontent.com"; // 반드시 수정
|
const clientId =
|
||||||
|
"16435018183-9a880bertda0en85387ge8f8mgsves71.apps.googleusercontent.com"; // 반드시 수정
|
||||||
const redirectUri = "https://google-oauth-access-token-whs.hako.li/callback";
|
const redirectUri = "https://google-oauth-access-token-whs.hako.li/callback";
|
||||||
|
|
||||||
const authUrl = "https://accounts.google.com/o/oauth2/v2/auth?" +
|
const authUrl =
|
||||||
|
"https://accounts.google.com/o/oauth2/v2/auth?" +
|
||||||
`client_id=${clientId}` +
|
`client_id=${clientId}` +
|
||||||
`&redirect_uri=${redirectUri}` +
|
`&redirect_uri=${redirectUri}` +
|
||||||
`&response_type=token` +
|
`&response_type=token` +
|
||||||
|
|
@ -20,20 +21,35 @@ app.get("/", (req, res) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
// Access Token 수신용 엔드포인트
|
// Access Token 수신용 엔드포인트
|
||||||
app.post("/token", async (req, res) => {
|
app.get("/token", async (req, res) => {
|
||||||
const token = req.body.access_token;
|
const token = req.query.access_token;
|
||||||
try {
|
try {
|
||||||
const response = await fetch("https://www.googleapis.com/oauth2/v3/userinfo", {
|
const response = await fetch(
|
||||||
|
"https://www.googleapis.com/oauth2/v3/userinfo",
|
||||||
|
{
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${token}`,
|
Authorization: `Bearer ${token}`,
|
||||||
},
|
},
|
||||||
});
|
}
|
||||||
|
);
|
||||||
const userInfo = await response.json();
|
const userInfo = await response.json();
|
||||||
console.log("Email:", userInfo.email);
|
console.log("Email:", userInfo.email);
|
||||||
console.log("Name:", userInfo.name);
|
console.log("Name:", userInfo.name);
|
||||||
console.log("Access Token:", token);
|
console.log("Access Token:", token);
|
||||||
|
|
||||||
res.send("Token received!");
|
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) {
|
} catch (err) {
|
||||||
console.error("❌ Error:", err);
|
console.error("❌ Error:", err);
|
||||||
res.status(500).send("Error");
|
res.status(500).send("Error");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue