jamalex / notion-py

Unofficial Python API client for Notion.so
MIT License
4.28k stars 476 forks source link

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

Open ivancruzbht opened 4 years ago

ivancruzbht commented 4 years ago

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?

kevinjalbert commented 4 years ago

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.

ekivolowitz commented 4 years ago

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.

ndrewwang commented 4 years ago

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

ekivolowitz commented 4 years ago

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

brdbry commented 4 years ago

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 commented 4 years ago

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.

ndrewwang commented 4 years ago

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

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

fedeserbin commented 4 years ago

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!!

ekivolowitz commented 4 years ago

@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.

osbre commented 4 years ago

I got the same error today. No code changed.

melMass commented 3 years ago

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

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

lennart02 commented 1 year ago

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

max0913 commented 1 year ago

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