I am implementing API login with node js and javascript. I'm trying to get a token, but the console says Object: null prototype as shown below.
[Symbol(Response internals)]: {
url: 'https://URL(sensitive information)',
status: 200,
statusText: 'OK',
headers: Headers { [Symbol(map)]: [Object: null prototype] },
counter: 0
}
}
Below is my code.
export const startKakaoLogin = (req, res) => {
const baseUrl = "https://kauth.kakao.com/oauth/authorize?";
const config = {
client_id: process.env.KA_ID,
redirect_uri: process.env.KA_RE,
response_type: "code",
prompt: "login",
};
const params = new URLSearchParams(config).toString();
const finalUrl = `${baseUrl}&${params}`;
return res.redirect(finalUrl);
};
export const finishKakaoLogin = async (req, res) => {
const baseUrl = "https://kauth.kakao.com/oauth/token";
const config = {
grant_type: "authorization_code",
client_id: process.env.KA_ID,
redirect_uri: process.env.KA_RE,
code: req.query.code,
};
const params = new URLSearchParams(config).toString();
const finalUrl = `${baseUrl}&${params}`;
const tokenRequest = await fetch(finalUrl, {
method: "POST",
headers: {
Accept: "application/x-www-form-urlencoded;charset=utf-8",
},
body: finalUrl,
});
console.log(tokenRequest);
};
Additionally, In the reference, 'curl' was used, but I used 'fetch', did I do something wrong?
POST /oauth/token HTTP/1.1
Host: kauth.kakao.com
Content-type: application/x-www-form-urlencoded;charset=utf-8
curl -v -X POST "https://kauth.kakao.com/oauth/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=authorization_code" \
-d "client_id=${REST_API_KEY}" \
--data-urlencode "redirect_uri=${REDIRECT_URI}" \
-d "code=${AUTHORIZE_CODE}"
What are you expecting to get from the endpoint? You're just logging the Response object right now and are seeing a representation of its internals.
To get e.g. the text from the response body, you'll need to parse it too, here with Response#text():
const text = await tokenRequest.text();
console.log(text);
If you're looking for JSON, then use Response#json().
const data = await tokenRequest.json();
console.log(data);
Be sure to check that the response's error code is what you expect, though, with e.g. tokenRequest.ok.