Troubleshooting Notion API OAuth: 'invalid client' error

I’m having trouble with Notion API OAuth. It works fine in Postman, but not in my app. Here’s what I’ve tried:

app.get('/notion-auth', (req, res) => {
  const authUrl = `${NOTION_AUTH_ENDPOINT}?owner=user&client_id=${CLIENT_ID}&response_type=code&redirect_uri=${encodeURI(REDIRECT_URI)}`;
  res.redirect(authUrl);
});

app.get('/notion-callback', async (req, res) => {
  try {
    const { code } = req.query;
    if (!code) throw new Error('Missing code from Notion');

    const response = await fetch('https://api.notion.com/v1/oauth/token', {
      method: 'POST',
      headers: {
        'Authorization': `Basic ${Buffer.from(`${CLIENT_ID}:${CLIENT_SECRET}`).toString('base64')}`,
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        grant_type: 'authorization_code',
        code,
        redirect_uri: REDIRECT_URI
      })
    });

    if (!response.ok) throw new Error(`HTTP error ${response.status}`);

    const data = await response.json();
    // Handle successful response

  } catch (error) {
    console.error(error);
    res.status(400).json({ error: 'OAuth failed' });
  }
});

I keep getting ‘invalid_client’ (401 error). I’ve double-checked the client ID, secret, and redirect URI. Any ideas what might be wrong? Thanks for any help!

hey have u double-checked ur redirect uri in the notion api settings? sometimes it needs to match EXACTLY whats in ur code. also make sure ur not using any test credentials by accident. those can cause weird errors. good luck!

Have you verified that your OAuth integration is properly set up in the Notion developers portal? Sometimes the issue lies there rather than in the code itself. Make sure your integration has the necessary scopes enabled for what you’re trying to do.

Another thing to check is the format of your client ID and secret when encoding them. Some developers encounter issues with special characters or whitespace. Try trimming any potential extra spaces before encoding.

If you’re still stuck, enabling more detailed error logging on your server might provide additional insights. Notion’s error messages can sometimes be a bit cryptic, but more verbose logs could reveal the root cause.

Lastly, if all else fails, try implementing the OAuth flow using a different library or even plain XMLHttpRequest. This can help isolate whether the issue is with your implementation or something on Notion’s end.

I’ve run into this ‘invalid_client’ issue before with Notion’s OAuth. One thing that helped me was ensuring the redirect URI was HTTPS, even for localhost testing. Notion can be picky about that.

Also, double-check your client ID and secret are for the correct environment (development vs. production). I once spent hours debugging only to realize I was using my dev credentials in production.

If those don’t work, try clearing your browser cache and cookies. Sometimes old OAuth tokens can interfere.

Lastly, Notion’s support team is surprisingly responsive. If you’re still stuck, shoot them an email with your client ID and they can often pinpoint the issue quickly.