I’m having trouble with Notion OAuth implementation. The authentication flow works perfectly when I test it through Postman, but fails in my actual app with an invalid_client error.
Here’s my current setup:
app
.get("/auth/notion", async (req, res) => {
const authURL = `${process.env.NOTION_AUTH_ENDPOINT}?owner=user&client_id=${process.env.CLIENT_ID}&response_type=code&redirect_uri=${encodeURIComponent(process.env.REDIRECT_URL)}`
res.redirect(authURL)
})
.get("/auth/notion/redirect", async (req, res) => {
try {
const {code} = req.query;
if(!code) {
throw new Error("Authorization code missing")
}
const tokenRequest = await axios.post('https://api.notion.com/v1/oauth/token', {
payload: JSON.stringify({
"grant_type": "authorization_code",
"code": code,
"redirect_uri": "http://localhost:9000/auth/notion/redirect"
}),
headers: {
"Authorization": `Basic ${Buffer.from(`${process.env.CLIENT_ID}:${process.env.CLIENT_SECRET}`).toString('base64')}`,
"Content-Type": "application/json",
}
})
const {access_token, bot_id, workspace_id} = tokenRequest.data;
// additional logic here
} catch(error) {
if(axios.isAxiosError(error)) {
console.log(error.response.data, error.response.status)
}
return res.status(400).json({error: `Auth failed: ${error}`})
}
})
I keep getting { error: 'invalid_client' } 401 response. I’ve double-checked my client credentials and redirect URI multiple times. Any ideas what could be wrong?