인프런 커뮤니티 질문&답변
구글 로그인 질문입니다.
작성
·
56
0
vue3에서 quasar 프레임워크 사용중인데 구글 로그인을 하고 나면 콘솔에
POST http://localhost:9000/token net::ERR_ABORTED 404 (Not Found)
이런 오류가 뜨는데 해결 가능할까요?
<template>
<div>
<button @click="redirectToGoogle">Login with Google</button>
</div>
</template>
<script>
export default {
methods: {
redirectToGoogle() {
const clientId =
'952226683996-giormhm5n1ch6vig4n2h0ng5vc9t0p0h.apps.googleusercontent.com';
const redirectUri = 'http://localhost:9000/';
const scope = 'https://www.googleapis.com/auth/userinfo.profile';
const responseType = 'code';
const url = `https://accounts.google.com/o/oauth2/v2/auth?client_id=${clientId}&redirect_uri=${redirectUri}&scope=${scope}&response_type=${responseType}`;
window.location.href = url;
},
async exchangeCodeForToken(code) {
try {
const response = await fetch('http://localhost:9000/token', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ code }),
});
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
console.log(data);
} catch (error) {
console.error('Error:', error);
}
},
created() {
window.addEventListener('message', event => {
if (event.data && event.data.code) {
this.exchangeCodeForToken(event.data.code);
}
});
},
},
};
</script>
<script>
export default {
methods: {
redirectToGoogle() {
const clientId =
'952226683996-giormhm5n1ch6vig4n2h0ng5vc9t0p0h.apps.googleusercontent.com';
const redirectUri = 'http://localhost:9000/';
const scope = 'https://www.googleapis.com/auth/userinfo.profile';
const responseType = 'code';
const url = `https://accounts.google.com/o/oauth2/v2/auth?client_id=${clientId}&redirect_uri=${redirectUri}&scope=${scope}&response_type=${responseType}`;
window.location.href = url;
},
async exchangeCodeForToken(code) {
try {
const response = await fetch('http://localhost:9000/token', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ code }),
});
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
console.log(data);
} catch (error) {
console.error('Error:', error);
}
},
},
created() {
const urlParams = new URLSearchParams(window.location.search);
const code = urlParams.get('code');
if (code) {
this.exchangeCodeForToken(code);
}
},
};
</script>const express = require('express');
const fetch = require('node-fetch');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
const PORT = 9000;
app.use(cors());
app.use(bodyParser.json());
app.post('/token', async (req, res) => {
const code = req.body.code;
const clientId =
'952226683996-giormhm5n1ch6vig4n2h0ng5vc9t0p0h.apps.googleusercontent.com';
const clientSecret = 'secret';
const redirectUri = 'http://localhost:9000/';
const tokenEndpoint = 'https://oauth2.googleapis.com/token';
const params = new URLSearchParams();
params.append('code', code);
params.append('client_id', clientId);
params.append('client_secret', clientSecret);
params.append('redirect_uri', redirectUri);
params.append('grant_type', 'authorization_code');
try {
const response = await fetch(tokenEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: params,
});
if (!response.ok) {
throw new Error('Failed to fetch token');
}
const data = await response.json();
res.json(data);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
답변 1
0
안녕하세요, 인프런 AI 인턴입니다. 박승민님 질문하신 내용과 관련하여, POST http://localhost:9000/token net::ERR_ABORTED 404 (Not Found) 오류가 발생하는 이유는 서버에서 ‘/token’ 경로로 설정된 POST 요청 처리 로직이 없거나 제대로 작동하지 않아서 발생할 수 있습니다. 제공해주신 서버 코드를 보면, ‘http://localhost:9000/token’ 경로에 대한 POST 요청을 처리하는 라우트가 존재하고 있습니다. 따라서, 문제는 다음과 같은 몇 가지 이유로 인해 발생할 수 있습니다.
- 서버가 실행되지 않았거나 잘못 실행되어 서버가 요청을 수신하고 있지 않을 수 있습니다. 서버가 정상적으로 실행되고 있는지 확인해주세요.
- 서버 코드에 변경사항이 있었다면, 서버를 재시작하여 변경 사항을 적용했는지 확인해주세요.
- 서버의 포트 번호나 경로가 잘못 설정되어 있지 않은지 확인해주세요. 제공하신 코드는 포트 9000에서 ‘/token’ 경로로 대기하고 있습니다.
- 개발 환경의 네트워크 문제로 서버에 접근할 수 없는 경우도 있으므로, 네트워크 연결 상태를 확인해주세요.
이 문제를 해결하기 위해 다음의 단계를 시도해볼 수 있습니다:
- 서버가 정상적으로 실행되어 있는지 확인하고, 필요하다면 서버를 재시작해보세요.
- 클라이언트 측 코드(debugger를 사용하여 로컬 개발 환경에서)에서 서버로의 요청이 올바르게 전송되고 있는지 확인해보세요.
- 서버 로그를 확인하여 ‘/token’ 경로로 들어오는 요청이 있는지, 있을 경우 어떤 응답을 보내고 있는지 확인해보세요.
- 포스트맨(Postman) 같은 API 테스트 도구를 사용해 ‘/token’ 경로에 대한 요청을 직접 해보며 서버 응답을 확인해보세요.
부디 이 단계들이 문제 해결에 도움이 되길 바랍니다.




