pyinat / pyinaturalist

Python client for iNaturalist
https://pyinaturalist.readthedocs.io
MIT License
133 stars 16 forks source link

Checking if the token provided is valid #541

Closed arky closed 8 months ago

arky commented 9 months ago

I have get the token manually from this page and save it environment everytime I run my notebook. The token is valid for 24 hours, is there a function in pyinaturalist to check if the token is valid or not.

Currently I notice that pyinaturalist doesn't provide a descriptive error when an invalid or expired token is used.

JWCook commented 9 months ago

For error messages, currently you should see something like this:

requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://api.inaturalist.org/v1/observations

I don't currently know of a way to differentiate between a 401 error due to an invalid token vs something you shouldn't have access to (like modifying another user's observations).

I could definitely add a function to check if a token is valid, though.

You might already be aware, but there are some other authentication features documented here, like keyring integration. Those do require creating an iNat Application, though, which is a bit inconvenient. You used to be able to get one instantly, but now you have to apply for one (I'm guessing because someone abused it at some point).

You mentioned you're running this in a notebook; are you using Jupyter or something else that runs in the browser? There might be an easier way to refresh access tokens without needing to copy/paste it every 24 hours. I should have some time to look into that later next week.

arky commented 9 months ago

@JWCook This issue could be resolved by writing documentation. I could help with it, now that I have spent a week looking into pyinaturalist.

Creating iNat Application requires you to have significant contributions so I choose to go with the grabbing API token route which seems less painless.

JWCook commented 8 months ago

I added a few more notes about authentication to the docs, but any contributions to that would be welcome!