OAuth2 "invalid_grant"?

I am trying to use OAuth2 for a Notion integration, but keep getting the error “invalid_grant”. I can not seem to figure out what I am doing wrong, any help would be appreciated.

app.get('/auth', (req, res) => {
	let query = req.query;
	console.log(query);

	if ('code' in query) {
		res.sendFile(`${__dirname}/pages/auth.html`);

		let auth = Buffer.from(`${process.env.CLIENT_ID}:${process.env.CLIENT_SECRET}`)
			.toString('base64');

		let options = {
			method: 'post',
			url: 'https://api.notion.com/v1/oauth/token',
			headers: {
				Accept: 'application/json',
				'Content-Type': 'application/json',
				Authorization: `Basic ${auth}`
			},
			data: {
				grant_type: 'authorization_code',
				code: query.code,
				redirect_uri: 'https://nodeserver.micahrao.repl.co'
			}
		};

		axios
			.request(options)
			.then(res => console.log(res.data))
			.catch(err => console.log(err));
		
	} else res.redirect('/');
});

This is the error that I get:

data: { error: 'invalid_grant', error_description: 'Invalid code.' }

Finally realized that the redirect_uri was incorrect.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.