jmaspons / osmapiR

An R interface to OpenStreetMap API for fetching and saving data from/to the OpenStreetMap database
https://jmaspons.github.io/osmapiR/
GNU General Public License v3.0
5 stars 3 forks source link

Implement OAuth2 authentication #1

Closed jmaspons closed 12 months ago

jmaspons commented 12 months ago

I tried to make the authentication without the OAuth secret (see below), but it fails. So the secret is saved obfuscated.

library(osmapiR)
#> Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright

token_url <- httr2::req_url_path(
  req = httr2::request(base_url = getOption("osmapir.base_auth_url")),
  "oauth2", "token"
)$url

client <- httr2::oauth_client(
  id = getOption("osmapir.oauth_id"),
  token_url = token_url,
  name = "osmapiR"
)

auth_url <- httr2::req_url_path(
  req = httr2::request(base_url = getOption("osmapir.base_auth_url")),
  "oauth2", "authorize"
)$url

token <- httr2::oauth_flow_auth_code(
  client = client,
  auth_url = auth_url,
  scope = paste(c("read_prefs", "write_prefs", "write_api", "read_gpx", "write_gpx", "write_notes"), collapse = " "),
  pkce = TRUE,
  host_name = "127.0.0.1"
)
#> Waiting for authentication in browser...
#> Press Esc/Ctrl + C to abort
#> Error in `oauth_flow_abort()`:
#> ! OAuth failure [invalid_client]
#> • Client authentication failed due to unknown client, no client authentication included, or unsupported authentication method.
#> Run `rlang::last_trace()` to see where the error occurred.
token
#> Error in eval(expr, envir, enclos): object 'token' not found

logout_osmapi()
jmaspons commented 12 months ago

Seems that it should work without the secret https://github.com/openstreetmap/openstreetmap-website/issues/3494#issuecomment-1330487750