I’m trying to integrate with Notion’s OAuth API, but I’m running into an issue when attempting to fetch the access token using a POST request. My code snippet is as follows:
const authHeader = Buffer.from(`${CLIENT_ID}:${CLIENT_SECRET}`).toString("base64");
const tokenResponse = await fetch("https://api.notion.com/v1/oauth/token", {
method: "POST",
headers: {
Authorization: `Basic ${authHeader}`,
"Content-Type": "application/json",
"Notion-Version": "2022-06-28",
Accept: "application/json",
},
body: JSON.stringify({
grant_type: "authorization_code",
code: authCode,
redirect_uri: "http://localhost:3000/api/notion/callback",
}),
});
if (!tokenResponse.ok) {
// Log response details to help with debugging
const text = await tokenResponse.text(); // Read response as plain text
console.error("Error response: ", text);
throw new Error(`Request failed with status ${tokenResponse.status}`);
}
However, I’m getting the following error:
Error response:
<!DOCTYPE html>
<html lang="en">
<head> <meta charset="utf-8"> <title>Error</title> </head>
<body> <pre>Cannot POST /v1/oauth/token</pre> </body>
</html>
- Made sure the URL https://api.notion.com/v1/oauth/token is correct based on Notion’s API documentation.
- Confirmed that CLIENT_ID and CLIENT_SECRET are correctly populated and encoded in base64.
- Verified that grant_type, code, and redirect_uri parameters are correctly set in the request body.