Search code examples
rapioauthrfigshare

Authentication error when using Figshare API via rfigshare


According to the Rfigshare readme:,

The first time you use an rfigshare function, it will ask you to authenticate online. Just log in and click okay to authenticate rfigshare. R will allow you to cache your login credentials so that you won't be asked to authenticate again (even between R sessions), as long as you are using the same working directory in future.

After installing rfigshare on a fresh machine (without an existing .httr-oauth)

library(devtools) 
install_github('ropensci/rfigshare')
library(rfigshare)

id = 3761562
fs_browse(id)

Error in value[[3L]](cond) : Requires authentication.
       Are your credentials stored in options?
       See fs_auth function for details.

Thus, in spite of what the readme says, I am not asked to authenticate.

Directly calling fs_auth does not work either:

> fs_auth()
Error in init_oauth1.0(self$endpoint, self$app, permission = self$params$permission,  :
  Bad Request (HTTP 400).

My sessionInfo is as follows:

sessionInfo()

R version 4.0.5 (2021-03-31)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 10.16

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] rfigshare_0.3.7.100

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.6       magrittr_2.0.1   tidyselect_1.1.0 munsell_0.5.0
 [5] colorspace_2.0-1 R6_2.5.0         rlang_0.4.11     fansi_0.5.0
 [9] httr_1.4.2       dplyr_1.0.5      grid_4.0.5       gtable_0.3.0
[13] utf8_1.2.1       DBI_1.1.1        ellipsis_0.3.2   assertthat_0.2.1
[17] yaml_2.2.1       tibble_3.1.2     lifecycle_1.0.0  crayon_1.4.1
[21] RJSONIO_1.3-1.4  purrr_0.3.4      ggplot2_3.3.3    later_1.2.0
[25] vctrs_0.3.8      promises_1.2.0.1 glue_1.4.2       compiler_4.0.5
[29] pillar_1.6.1     generics_0.1.0   scales_1.1.1     XML_3.99-0.6
[33] httpuv_1.6.1     pkgconfig_2.0.3

Does anyone have any tips or workarounds? This definitely did work maybe 6 months ago when I last tried. I also have an open thread about this issue with Figshare support, but their knowledge of the R library seems limited.

(cross-posted from Github)


Solution

  • You might also consider leveraging the fact that the current figshare api is Open API compatible and build your own client on the fly with the swagger specification.

    Generate and store a personal access token as I described in my other answer. Then you could do

    library(rapiclient)
    library(httr)
    
    fs_api <- get_api("https://docs.figshare.com/swagger.json")
    header <- c(Authorization = sprintf("token %s", Sys.getenv("RFIGSHARE_PAT")))
    fs_api <- list(operations = get_operations(fs_api, header), 
                   schemas = get_schemas(fs_api))
    
    my_articles <- fs_api$operations$private_articles_list()
    content(my_articles)