munterfi / hereR

R package that provides an interface to the HERE REST APIs: Geocoder API, Routing API, Traffic API, Public Transit API and Destination Weather API. Locations and routes are returned as 'sf' objects.
https://munterfi.github.io/hereR/
GNU General Public License v3.0
89 stars 11 forks source link

Bug: `flow` function return 403 #158

Closed isaid-hi closed 1 year ago

isaid-hi commented 1 year ago

Describe the bug flow function return 403. the API Key have no problem because I tried using httr and it return 200.

Reproducible example

library(hereR)
set_key(here_api_key)
flow(aoi, min_jam_factor = 0)

# NULL
# Warning messages:
#   1: In .parse_response(i, out$responses()[[i]]) :
#   https://traffic.ls.hereapi.com/traffic/6.2/flow.json: Request 'id = 1' failed. 
# Status 403; Forbidden; Request forbidden -- authorization will not help.
# 2: In .parse_response(i, out$responses()[[i]]) :
#   https://traffic.ls.hereapi.com/traffic/6.2/flow.json: Request 'id = 2' failed. 
# Status 403; Forbidden; Request forbidden -- authorization will not help.

Contrasting to this one using httr

url <- "https://data.traffic.hereapi.com/v7/flow?in=circle:52.50811,13.47853;r=2000&locationReferencing=olr&apiKey="
complete_url <- paste0(url, here_api_key)
response <- GET(complete_url)

# Response [https://data.traffic.hereapi.com/v7/flow?in=circle:52.50811,13.47853;r=2000&locationReferencing=olr&apiKey={here_api_key}]
# Date: 2023-07-08 15:18
# Status: 200
# Content-Type: application/json
# Size: 49.3 kB

Expected behavior This clearly not API key as expected in #157 issue

Thanks for reporting, the issue you're experiencing is likely caused by an invalid API key.

Go to the projects view on HERE developer. Then create a new project and select REST. There you should be able to create an API key using the button “Create API key”. Copy paste the key and set it using set_key(...). Or alternatively export it to the environment export HERE_API_KEY=<YOUR-VALID-KEY-HERE>.

Additionally I'm not sure, but is this package using flow API v6? In that case, this might be because its on maintenance

munterfi commented 1 year ago

Thanks for opening a new issue considering the flow endpoint.

Yes, hereR is using the older HERE Traffic API v6.

With an API key, generated via the older HERE Developer, flow() works as expected:

# libraries
library(dplyr)
library(sf)
library(mapview)
library(hereR)

# use a valid key from HERE Developer
# set_key("<KEY>")

# print some logs
set_verbose(TRUE)

# request traffic flow information
flow_data <- aoi |>
  filter(code == "LI") |>
  flow(min_jam_factor = 0)
#> Sending 1 request(s) with 10 RPS to: 'https://traffic.ls.hereapi.com/traffic/6.2/flow.json?...'
#> Received 1 response(s) with total size: 336.9 Kb

flow_data
#> Simple feature collection with 9 features and 10 fields
#> Geometry type: MULTILINESTRING
#> Dimension:     XY
#> Bounding box:  xmin: 9.47778 ymin: 47.07055 xmax: 9.52095 ymax: 47.24551
#> Geodetic CRS:  WGS 84
#>   id    PC       DE QD      LE TY     SP    FF      JF   CN
#> 1  1 27109     Haag  + 0.28930 TR  25.25  46.1 3.44178 0.90
#> 2  1 24659     Haag  + 0.34188 TR  23.52  37.5 3.31472 0.88
#> 3  1 27109     Haag  - 0.63119 TR  22.80  42.2 3.14159 0.91
#> 4  1 24661   Schaan  - 1.63900 TR  29.78  46.4 3.18368 0.77
#> 5  1 24661   Schaan  + 0.07961 TR  35.28  35.0 0.00000 0.81
#> 6  1 24663    Vaduz  + 0.03241 TR  17.22  29.3 2.75528 0.81
#> 7  1 24534 Trübbach  - 0.10905 TR  32.00  40.0 1.77777 0.70
#> 8  1 10328 Sennwald  - 3.90001 TR 120.00 105.0 0.00000 0.99
#> 9  1 10329     Haag  + 3.91267 TR 116.23 105.0 0.00000 0.99
#>                         geometry
#> 1 MULTILINESTRING ((9.49716 4...
#> 2 MULTILINESTRING ((9.49344 4...
#> 3 MULTILINESTRING ((9.48906 4...
#> 4 MULTILINESTRING ((9.48909 4...
#> 5 MULTILINESTRING ((9.51056 4...
#> 6 MULTILINESTRING ((9.52095 4...
#> 7 MULTILINESTRING ((9.47778 4...
#> 8 MULTILINESTRING ((9.49777 4...
#> 9 MULTILINESTRING ((9.51545 4...

However, with an API key from the HERE platform I can reproduce the error:

# use a valid key from HERE Platform
# set_key("<KEY>")

# request traffic flow information
flow_data <- aoi |>
  filter(code == "LI") |>
  flow(min_jam_factor = 0)
#> Sending 1 request(s) with 10 RPS to: 'https://traffic.ls.hereapi.com/traffic/6.2/flow.json?...'
#> Warning in .parse_response(i, out$responses()[[i]]): https://traffic.ls.hereapi.com/traffic/6.2/flow.json: Request 'id = 1' failed. 
#>   Status 403; Forbidden; Request forbidden -- authorization will not help.
#> Received 1 response(s) with total size: 288 bytes

The HTTP error 403 means that the client has valid credentials but not enough privileges to perform an action on a resource. Possibly with newer keys from the platform, the older API version is no longer supported. This would mean that hereR would have to update to a newer version of the endpoint.

Session info ``` r devtools::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.3.1 (2023-06-16) #> os macOS Ventura 13.4.1 #> system x86_64, darwin20 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz Europe/Zurich #> date 2023-07-11 #> pandoc 3.1.2 @ /usr/local/bin/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> base64enc 0.1-3 2015-07-28 [1] CRAN (R 4.3.0) #> cachem 1.0.8 2023-05-01 [1] CRAN (R 4.3.0) #> callr 3.7.3 2022-11-02 [1] CRAN (R 4.3.0) #> class 7.3-22 2023-05-03 [1] CRAN (R 4.3.1) #> classInt 0.4-9 2023-02-28 [1] CRAN (R 4.3.0) #> cli 3.6.1 2023-03-23 [1] CRAN (R 4.3.0) #> codetools 0.2-19 2023-02-01 [1] CRAN (R 4.3.1) #> colorspace 2.1-0 2023-01-23 [1] CRAN (R 4.3.0) #> crayon 1.5.2 2022-09-29 [1] CRAN (R 4.3.0) #> crosstalk 1.2.0 2021-11-04 [1] CRAN (R 4.3.0) #> crul 1.4.0 2023-05-17 [1] CRAN (R 4.3.0) #> curl 5.0.1 2023-06-07 [1] CRAN (R 4.3.0) #> data.table 1.14.8 2023-02-17 [1] CRAN (R 4.3.0) #> DBI 1.1.3 2022-06-18 [1] CRAN (R 4.3.0) #> devtools 2.4.5 2022-10-11 [1] CRAN (R 4.3.0) #> digest 0.6.32 2023-06-26 [1] CRAN (R 4.3.0) #> dplyr * 1.1.2 2023-04-20 [1] CRAN (R 4.3.0) #> e1071 1.7-13 2023-02-01 [1] CRAN (R 4.3.0) #> ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.3.0) #> evaluate 0.21 2023-05-05 [1] CRAN (R 4.3.0) #> fansi 1.0.4 2023-01-22 [1] CRAN (R 4.3.0) #> fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.3.0) #> fs 1.6.2 2023-04-25 [1] CRAN (R 4.3.0) #> generics 0.1.3 2022-07-05 [1] CRAN (R 4.3.0) #> glue 1.6.2 2022-02-24 [1] CRAN (R 4.3.0) #> hereR * 0.9.1 2023-01-09 [1] CRAN (R 4.3.0) #> htmltools 0.5.5 2023-03-23 [1] CRAN (R 4.3.0) #> htmlwidgets 1.6.2 2023-03-17 [1] CRAN (R 4.3.0) #> httpcode 0.3.0 2020-04-10 [1] CRAN (R 4.3.0) #> httpuv 1.6.11 2023-05-11 [1] CRAN (R 4.3.0) #> jsonlite 1.8.7 2023-06-29 [1] CRAN (R 4.3.0) #> KernSmooth 2.23-21 2023-05-03 [1] CRAN (R 4.3.1) #> knitr 1.43 2023-05-25 [1] CRAN (R 4.3.0) #> later 1.3.1 2023-05-02 [1] CRAN (R 4.3.0) #> lattice 0.21-8 2023-04-05 [1] CRAN (R 4.3.1) #> leafem 0.2.0 2022-04-16 [1] CRAN (R 4.3.0) #> leaflet 2.1.2 2023-03-10 [1] CRAN (R 4.3.0) #> lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.3.0) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.3.0) #> mapview * 2.11.0 2022-04-16 [1] CRAN (R 4.3.0) #> memoise 2.0.1 2021-11-26 [1] CRAN (R 4.3.0) #> mime 0.12 2021-09-28 [1] CRAN (R 4.3.0) #> miniUI 0.1.1.1 2018-05-18 [1] CRAN (R 4.3.0) #> munsell 0.5.0 2018-06-12 [1] CRAN (R 4.3.0) #> pillar 1.9.0 2023-03-22 [1] CRAN (R 4.3.0) #> pkgbuild 1.4.2 2023-06-26 [1] CRAN (R 4.3.0) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.3.0) #> pkgload 1.3.2 2022-11-16 [1] CRAN (R 4.3.0) #> png 0.1-8 2022-11-29 [1] CRAN (R 4.3.0) #> prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.3.0) #> processx 3.8.2 2023-06-30 [1] CRAN (R 4.3.0) #> profvis 0.3.8 2023-05-02 [1] CRAN (R 4.3.0) #> promises 1.2.0.1 2021-02-11 [1] CRAN (R 4.3.0) #> proxy 0.4-27 2022-06-09 [1] CRAN (R 4.3.0) #> ps 1.7.5 2023-04-18 [1] CRAN (R 4.3.0) #> purrr 1.0.1 2023-01-10 [1] CRAN (R 4.3.0) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.3.0) #> raster 3.6-23 2023-07-04 [1] CRAN (R 4.3.0) #> Rcpp 1.0.10 2023-01-22 [1] CRAN (R 4.3.0) #> remotes 2.4.2 2021-11-30 [1] CRAN (R 4.3.0) #> rlang 1.1.1 2023-04-28 [1] CRAN (R 4.3.0) #> rmarkdown 2.23 2023-07-01 [1] CRAN (R 4.3.0) #> rstudioapi 0.14 2022-08-22 [1] CRAN (R 4.3.0) #> s2 1.1.4 2023-05-17 [1] CRAN (R 4.3.0) #> satellite 1.0.4 2021-10-12 [1] CRAN (R 4.3.0) #> scales 1.2.1 2022-08-20 [1] CRAN (R 4.3.0) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.3.0) #> sf * 1.0-13 2023-05-24 [1] CRAN (R 4.3.0) #> shiny 1.7.4 2022-12-15 [1] CRAN (R 4.3.0) #> sp 2.0-0 2023-06-22 [1] CRAN (R 4.3.0) #> stringi 1.7.12 2023-01-11 [1] CRAN (R 4.3.0) #> stringr 1.5.0 2022-12-02 [1] CRAN (R 4.3.0) #> terra 1.7-39 2023-06-23 [1] CRAN (R 4.3.0) #> tibble 3.2.1 2023-03-20 [1] CRAN (R 4.3.0) #> tidyselect 1.2.0 2022-10-10 [1] CRAN (R 4.3.0) #> units 0.8-2 2023-04-27 [1] CRAN (R 4.3.0) #> urlchecker 1.0.1 2021-11-30 [1] CRAN (R 4.3.0) #> usethis 2.2.1 2023-06-23 [1] CRAN (R 4.3.0) #> utf8 1.2.3 2023-01-31 [1] CRAN (R 4.3.0) #> vctrs 0.6.3 2023-06-14 [1] CRAN (R 4.3.0) #> webshot 0.5.5 2023-06-26 [1] CRAN (R 4.3.0) #> wk 0.7.3 2023-05-06 [1] CRAN (R 4.3.0) #> xfun 0.39 2023-04-20 [1] CRAN (R 4.3.0) #> xtable 1.8-4 2019-04-21 [1] CRAN (R 4.3.0) #> yaml 2.3.7 2023-01-23 [1] CRAN (R 4.3.0) #> #> [1] /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library #> #> ────────────────────────────────────────────────────────────────────────────── ```
munterfi commented 1 year ago

Upgraded to HERE Traffic API v7, which enables also newer keys from the HERE Platform to use flow():

# libraries
library(dplyr)
library(hereR)

# use a valid key from HERE Platform
# set_key("<KEY>")

# print some logs
set_verbose(TRUE)

# request traffic flow information
flow_data <- aoi |>
  filter(code == "LI") |>
  flow(min_jam_factor = 0)
#> Sending 1 request(s) with 10 RPS to: 'https://data.traffic.hereapi.com/v7/flow?...'
#> Received 1 response(s) with total size: 787 Kb

flow_data
#> Simple feature collection with 37 features and 7 fields
#> Geometry type: MULTILINESTRING
#> Dimension:     XY
#> Bounding box:  xmin: 9.43993 ymin: 47.03386 xmax: 9.57256 ymax: 47.30966
#> Geodetic CRS:  WGS 84
#> First 10 features:
#>    id    speed speed_uncapped free_flow jam_factor confidence traversability
#> 1   1 33.33334       34.44445  29.16667          0       0.79           open
#> 2   1 33.33334       34.16667  29.16667          0       0.86           open
#> 3   1 16.38889       16.38889  15.83333          0       0.70           open
#> 4   1 11.94444       11.94444  11.66667          0       0.70           open
#> 5   1 33.33334       33.61111  29.16667          0       0.77           open
#> 6   1 12.50000       12.50000  12.22222          0       0.74           open
#> 7   1 14.44444       14.44444  14.44444          0       0.71           open
#> 8   1 29.16667       29.16667  27.22222          0       0.79           open
#> 9   1 13.05556       13.05556  12.77778          0       0.70           open
#> 10  1 17.77778       17.77778  17.50000          0       0.73           open
#>                          geometry
#> 1  MULTILINESTRING ((9.49777 4...
#> 2  MULTILINESTRING ((9.49555 4...
#> 3  MULTILINESTRING ((9.49972 4...
#> 4  MULTILINESTRING ((9.48906 4...
#> 5  MULTILINESTRING ((9.48558 4...
#> 6  MULTILINESTRING ((9.5209 47...
#> 7  MULTILINESTRING ((9.47631 4...
#> 8  MULTILINESTRING ((9.48475 4...
#> 9  MULTILINESTRING ((9.49716 4...
#> 10 MULTILINESTRING ((9.46343 4...

Note: The column names in the output have changed.

Can you check with the development version of the package remotes::install_github("munterfi/hereR@feature/update-flow") if it solves the issue for you?

Session info ``` r devtools::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.3.1 (2023-06-16) #> os macOS Ventura 13.4.1 #> system x86_64, darwin20 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz Europe/Zurich #> date 2023-07-12 #> pandoc 3.1.2 @ /usr/local/bin/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> base64enc 0.1-3 2015-07-28 [1] CRAN (R 4.3.0) #> cachem 1.0.8 2023-05-01 [1] CRAN (R 4.3.0) #> callr 3.7.3 2022-11-02 [1] CRAN (R 4.3.0) #> class 7.3-22 2023-05-03 [1] CRAN (R 4.3.1) #> classInt 0.4-9 2023-02-28 [1] CRAN (R 4.3.0) #> cli 3.6.1 2023-03-23 [1] CRAN (R 4.3.0) #> codetools 0.2-19 2023-02-01 [1] CRAN (R 4.3.1) #> colorspace 2.1-0 2023-01-23 [1] CRAN (R 4.3.0) #> crayon 1.5.2 2022-09-29 [1] CRAN (R 4.3.0) #> crosstalk 1.2.0 2021-11-04 [1] CRAN (R 4.3.0) #> crul 1.4.0 2023-05-17 [1] CRAN (R 4.3.0) #> curl 5.0.1 2023-06-07 [1] CRAN (R 4.3.0) #> data.table 1.14.8 2023-02-17 [1] CRAN (R 4.3.0) #> DBI 1.1.3 2022-06-18 [1] CRAN (R 4.3.0) #> devtools 2.4.5 2022-10-11 [1] CRAN (R 4.3.0) #> digest 0.6.32 2023-06-26 [1] CRAN (R 4.3.0) #> dplyr * 1.1.2 2023-04-20 [1] CRAN (R 4.3.0) #> e1071 1.7-13 2023-02-01 [1] CRAN (R 4.3.0) #> ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.3.0) #> evaluate 0.21 2023-05-05 [1] CRAN (R 4.3.0) #> fansi 1.0.4 2023-01-22 [1] CRAN (R 4.3.0) #> fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.3.0) #> fs 1.6.2 2023-04-25 [1] CRAN (R 4.3.0) #> generics 0.1.3 2022-07-05 [1] CRAN (R 4.3.0) #> glue 1.6.2 2022-02-24 [1] CRAN (R 4.3.0) #> hereR * 0.9.1 2023-07-11 [1] Github (munterfi/hereR@ad52ebc) #> htmltools 0.5.5 2023-03-23 [1] CRAN (R 4.3.0) #> htmlwidgets 1.6.2 2023-03-17 [1] CRAN (R 4.3.0) #> httpcode 0.3.0 2020-04-10 [1] CRAN (R 4.3.0) #> httpuv 1.6.11 2023-05-11 [1] CRAN (R 4.3.0) #> jsonlite 1.8.7 2023-06-29 [1] CRAN (R 4.3.0) #> KernSmooth 2.23-21 2023-05-03 [1] CRAN (R 4.3.1) #> knitr 1.43 2023-05-25 [1] CRAN (R 4.3.0) #> later 1.3.1 2023-05-02 [1] CRAN (R 4.3.0) #> lattice 0.21-8 2023-04-05 [1] CRAN (R 4.3.1) #> leafem 0.2.0 2022-04-16 [1] CRAN (R 4.3.0) #> leaflet 2.1.2 2023-03-10 [1] CRAN (R 4.3.0) #> lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.3.0) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.3.0) #> mapview 2.11.0 2022-04-16 [1] CRAN (R 4.3.0) #> memoise 2.0.1 2021-11-26 [1] CRAN (R 4.3.0) #> mime 0.12 2021-09-28 [1] CRAN (R 4.3.0) #> miniUI 0.1.1.1 2018-05-18 [1] CRAN (R 4.3.0) #> munsell 0.5.0 2018-06-12 [1] CRAN (R 4.3.0) #> pillar 1.9.0 2023-03-22 [1] CRAN (R 4.3.0) #> pkgbuild 1.4.2 2023-06-26 [1] CRAN (R 4.3.0) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.3.0) #> pkgload 1.3.2 2022-11-16 [1] CRAN (R 4.3.0) #> png 0.1-8 2022-11-29 [1] CRAN (R 4.3.0) #> prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.3.0) #> processx 3.8.2 2023-06-30 [1] CRAN (R 4.3.0) #> profvis 0.3.8 2023-05-02 [1] CRAN (R 4.3.0) #> promises 1.2.0.1 2021-02-11 [1] CRAN (R 4.3.0) #> proxy 0.4-27 2022-06-09 [1] CRAN (R 4.3.0) #> ps 1.7.5 2023-04-18 [1] CRAN (R 4.3.0) #> purrr 1.0.1 2023-01-10 [1] CRAN (R 4.3.0) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.3.0) #> raster 3.6-23 2023-07-04 [1] CRAN (R 4.3.0) #> Rcpp 1.0.10 2023-01-22 [1] CRAN (R 4.3.0) #> remotes 2.4.2 2021-11-30 [1] CRAN (R 4.3.0) #> rlang 1.1.1 2023-04-28 [1] CRAN (R 4.3.0) #> rmarkdown 2.23 2023-07-01 [1] CRAN (R 4.3.0) #> rstudioapi 0.14 2022-08-22 [1] CRAN (R 4.3.0) #> s2 1.1.4 2023-05-17 [1] CRAN (R 4.3.0) #> satellite 1.0.4 2021-10-12 [1] CRAN (R 4.3.0) #> scales 1.2.1 2022-08-20 [1] CRAN (R 4.3.0) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.3.0) #> sf 1.0-13 2023-05-24 [1] CRAN (R 4.3.0) #> shiny 1.7.4 2022-12-15 [1] CRAN (R 4.3.0) #> sp 2.0-0 2023-06-22 [1] CRAN (R 4.3.0) #> stringi 1.7.12 2023-01-11 [1] CRAN (R 4.3.0) #> stringr 1.5.0 2022-12-02 [1] CRAN (R 4.3.0) #> terra 1.7-39 2023-06-23 [1] CRAN (R 4.3.0) #> tibble 3.2.1 2023-03-20 [1] CRAN (R 4.3.0) #> tidyselect 1.2.0 2022-10-10 [1] CRAN (R 4.3.0) #> units 0.8-2 2023-04-27 [1] CRAN (R 4.3.0) #> urlchecker 1.0.1 2021-11-30 [1] CRAN (R 4.3.0) #> usethis 2.2.1 2023-06-23 [1] CRAN (R 4.3.0) #> utf8 1.2.3 2023-01-31 [1] CRAN (R 4.3.0) #> vctrs 0.6.3 2023-06-14 [1] CRAN (R 4.3.0) #> webshot 0.5.5 2023-06-26 [1] CRAN (R 4.3.0) #> wk 0.7.3 2023-05-06 [1] CRAN (R 4.3.0) #> xfun 0.39 2023-04-20 [1] CRAN (R 4.3.0) #> xtable 1.8-4 2019-04-21 [1] CRAN (R 4.3.0) #> yaml 2.3.7 2023-01-23 [1] CRAN (R 4.3.0) #> #> [1] /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library #> #> ────────────────────────────────────────────────────────────────────────────── ```
isaid-hi commented 1 year ago

I see thanks for the maintenance. Apologize for not getting back sooner. Apparently, it ridiculously hard to sign up for HERE Developer (keep redirected to HERE Platform). And since API v7 didn't provide the road class data (need to "contact sales" to get it) make the v6 one more favorable for my work. Very disappointing from their end.

After all, thank you for updating to v7!