notion-py icon indicating copy to clipboard operation
notion-py copied to clipboard

401 Client Error: Unauthorized for url: https://www.notion.so/api/v3/loadUserContent

Open ivancruzbht opened this issue 5 years ago • 13 comments

I am getting this error when I am trying to instantiate a NotionClient with a token gotten from a browser cookie.

from notion.client import NotionClient

client = NotionClient(token)

I am running this from a Heroku app as explained in this blog post

My guess is that Notion is not longer allowing API usage outside their apps.

Anyone having the same issues?

ivancruzbht avatar Mar 28 '20 22:03 ivancruzbht

As of today, I'm still using notion-py without any issue. I suspect your token has just gone invalid, sometimes this happens. I would try fetching a fresh token and try this again.

kevinjalbert avatar Apr 03 '20 19:04 kevinjalbert

Agreed with @kevinjalbert. The token's expiration date is set to some time in 2051 IIRC. From what I've found, the only time the token refreshes is if you log out and log back in, or log in from a new source.

As long as you haven't signed out or logged in somewhere else, I'd be willing to bet that the same token would work indefinitely.

ekivolowitz avatar Apr 09 '20 04:04 ekivolowitz

How does one fetch a fresh token? I am also running into this error when following the tutorial on Mac OSX, notion logged in.

ndrewwang avatar Apr 25 '20 22:04 ndrewwang

@ndrewwang log out and log back into the browser, then copy the cookie.

ekivolowitz avatar Apr 25 '20 22:04 ekivolowitz

I was using this library fine for a few weeks until a couple of days ago (my token seemed to work fine across logout/logons), but now I'm getting this 401 error every time. I've tried logging in again a couple of times, both with Google auth and password, and copying the new token_v2 cookie, but now get "401 Client Error: Unauthorized for url: https://www.notion.so/api/v3/loadUserContent" every time I instantiate NotionClient.

@jamalex do you know if anything's changed with the API?

brdbry avatar Apr 27 '20 05:04 brdbry

Actually, what seems to have changed is that tokens are now expired on logging out of notion. I am using tokens from two different users in my app, and when I switched between them (logout 1, login 2) to get the second token, it was expiring the first! I got the second token using Safari instead of Chrome and that fixed it.

brdbry avatar Apr 27 '20 06:04 brdbry

Thanks, solved! Got to be careful when juggling multiple accounts.

Copied the token when viewing the Notion cookie from browser settings and replaced <token_v2> and it works!

ndrewwang avatar Apr 27 '20 11:04 ndrewwang

I am getting this error when deployed on Google App Engine. The token has my localhost url and that is the problem right?

How would you fix this? Is there a login resource in the API? I am working on integrating the company erp to our Notion workspace.

Thanks!!

fedeserbin avatar May 17 '20 21:05 fedeserbin

@fedeserbin the only thing you need is the token_v2 value. The token you've provided likely has been invalidated, which happens when you log out and back in again. If you log out and log back in again, then copy that value and deploy it on GAE, it should work.

There is no login resource in the API.

ekivolowitz avatar May 17 '20 21:05 ekivolowitz

I got the same error today. No code changed.

osbre avatar Aug 11 '20 10:08 osbre

Thanks for the solution (logging in & out) it works!!

@jamalex Maybe add it to the readme and close this issue?

melMass avatar Feb 14 '21 13:02 melMass

I have the same problem and even tried to log out and log in again.

lennart02 avatar Oct 25 '22 17:10 lennart02

I tried to connect with ChatGPT along with the notable plugin / notebook where I pasted the Notion API-Key and got the same problem.

max0913 avatar Jun 27 '23 12:06 max0913