I’m trying to implement Notion OAuth in my app but running into issues. After getting the authorization code from the callback, I’m attempting to exchange it for an access token but keep getting a 401 unauthorized response.
I’m using localhost as my redirect URI in the integration settings. Has anyone encountered this before? The Notion docs don’t seem to have much detail about troubleshooting this specific error.
Same exact issue hit me last month building my Notion integration. Your Authorization header’s the problem. Notion’s OAuth token endpoint needs Basic auth with your client credentials - don’t just throw them in the request body. Encode your client_id and client_secret as base64 and stick it in the Authorization header: ‘Authorization’: 'Basic ’ + btoa(myClientId + ‘:’ + myClientSecret). Double-check your redirect_uri matches exactly what you registered - protocol, trailing slashes, everything. That mismatch burned me for hours.
i had this same issue too! make sure ur redirect_uri is included in the request body. it can be the reason you’re getting 401, as Notion is picky about that. also, double check that it matches ur integration settings perfectly, even the http:// part.
The authentication header’s probably your issue. Notion’s OAuth token exchange needs Basic auth with your client credentials base64-encoded. Don’t put client_id and client_secret in the body - use the Authorization header instead:
Also, the content type needs to be form-urlencoded, not JSON. This tripped me up when I first worked with it since most APIs these days just take JSON.
Had the same issue building my CMS integration. Problem was my dev environment setup. Notion’s OAuth hates localhost redirects sometimes, even when they’re allowed in settings. Used ngrok to create an HTTPS tunnel for testing - fixed the 401 instantly. Also check if your integration got regenerated recently. If you changed anything in the Notion dev console, the client_secret might’ve changed without you knowing. That one got me and wasted way too much time.
check your headers - make sure you’re using the right notion api version. i got the same 401 errors bc i forgot to add ‘Notion-Version’: ‘2022-06-28’ to my token request. Notion’s api is super picky about versioning, even for oauth endpoints.
Check your OAuth settings in the Notion developer console - focus on the redirect URI. I hit this same 401 error and found that localhost redirects don’t play nice with Notion’s OAuth. Switch to 127.0.0.1 instead of localhost in both your integration settings and the redirect_uri parameter. Also make sure your auth code hasn’t expired - Notion’s codes die pretty fast and you’ve got to exchange them right after the callback. If you wait too long or try reusing it, the code goes invalid.
make sure your integration’s not disabled in the Notion console. also, URL-encode your auth code before sending it - special chars can be tricky, nothin’s worse than a finicky oauth!
Check your timestamp - Notion validates request timing on their OAuth endpoint. Too much delay between getting the auth code and exchanging it = 401 errors. Hit this myself while debugging because I was pausing with breakpoints. The auth code’s still valid, but Notion wants the token exchange to happen quickly. Also double-check your client credentials from the integration settings - make sure there’s no hidden unicode or whitespace. Some browsers and text editors sneak in invisible characters when you copy-paste, which breaks auth.
Check if your authorization code is still valid. I got the same 401 error because I’d already used my code once - Notion’s auth codes only work once and expire fast. Testing your callback multiple times? You’ll need a fresh auth code each time. Also double-check your client_secret from the integration settings - no extra spaces. One wrong character and you’ll hit 401 every time.