MushroomObserver / mushroom-observer

A website for sharing observations of mushrooms.
https://mushroomobserver.org
MIT License
77 stars 25 forks source link

Authenticate iNat import #2222

Open JoeCohen opened 2 weeks ago

JoeCohen commented 2 weeks ago

Authenticate iNat imports with iNat API Key and user handshake. For details and documentation see https://github.com/MushroomObserver/mushroom-observer/issues/1955#issuecomment-2016029239

Tasks

JoeCohen commented 2 weeks ago

Pulk's Mirror. https://github.com/JacobPulk/mirror

JoeCohen commented 2 weeks ago

rough outline:

  1. you redirect your user to iNat,
  2. the user is offered the choice to authorize your app with access to their iNat data, and
  3. If they agree, they are redirected to the redirect_uri you specified with an access_token in the params.

https://www.inaturalist.org/pages/api+reference#authorization_code_flow

JoeCohen commented 2 weeks ago

Questions:

JoeCohen commented 2 weeks ago

Created iNat application https://www.inaturalist.org/oauth/applications/851

JoeCohen commented 2 weeks ago

params in the callback action If iNat user approves the request:

(rdbg) params
#<ActionController::Parameters {"code"=>"NtX2QvCzQuwXAyKL0uDtvZLcWDLQpo4i5QWkAtTMT1g", "controller"=>"observations/inat_imports", "action"=>"auth"} permitted: false>

If iNat user denies the request:

(rdbg) params
#<ActionController::Parameters {"error"=>"access_denied", "error_description"=>"The resource owner or authorization server denied the request.", "controller"=>"observations/inat_imports", "action"=>"auth"} permitted: false>
JoeCohen commented 2 weeks ago

How to proceed after receiving code from iNat at the callback url? @nwilson-eol's suggestion:

JoeCohen commented 1 week ago

httparty readme

JoeCohen commented 1 week ago

20240719 0720 Pacific Not working as expected as of #29809c667b67d86d13445496aa4d59333f8f2a2

Update API request to === iNat docs

- fixes headers
- uses RestClient instead of Httparty
See https://www.inaturalist.org/pages/api+reference#authorization_code_flow

The response does not include private location. Probably because authentication isn't working. But there's a slim chance that iNat doesn't include private location even if the request is authenticated. But I tried setting a breakpoint in inat_search_observations right after ::Inat.new ... then manually

RestClient.get("#{API_BASE}/users/me", headers)
RestClient::Unauthorized: 401 Unauthorized
    from /Users/joe/mushroom-observer/app/classes/inat.rb:15:in `initialize'

I don't understand this. But I've been stuck on this a while. So I'll put it aside for the moment, and work on things that depend on authentication.

JoeCohen commented 5 days ago

Link to an old iNat app that imported MO Observations: https://www.inaturalist.org/posts/20053-18-mushroom-observer-api-key