通过 rfigshare 使用 Figshare API 时出现身份验证错误

Authentication error when using Figshare API via rfigshare

根据 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.

在新机器上安装 rfigshare 之后(没有现有的 .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.

因此,不管自述文件怎么说,我都没有被要求进行身份验证。

直接调用fs_auth也不行:

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

我的sessionInfo如下:

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

有人有任何提示或解决方法吗?大概 6 个月前,当我最后一次尝试时,这确实有效。我也有一个关于 Figshare 支持的问题的开放线程,但他们对 R 库的了解似乎有限。

(从 Github 交叉发布)

rfigsharemaster branch 似乎与 figshare 现在提供的功能不符,因为 master 分支似乎使用 api 的 v1 以及 oauth v1 身份验证,而figshare 继续使用 api 的 v2,现在推广使用 oauth v2。

虽然我不确定 figshare 是否已关闭 api and/or 的 v1 已禁止 oauth v1,但如果您从 sckott branch 并使用个人访问令牌 (PAT)。

要生成 PAT,请在网络浏览器中导航到 https://figshare.com/account/applications。在此页面底部,您可以生成 PAT。当令牌出现时,复制它,因为您将无法再次查看它(尽管您可以随时轻松生成一个新令牌)。

您需要将此令牌存储在您的 .Renviron 文件中。 usethis 包有一个漂亮的 edit_r_environ() 函数使这更容易一些:

usethis::edit_r_environ()

运行 R 中的上述内容应该可以找到您的 .Renviron 文件并打开它进行编辑。将您的 PAT 存储在新行上。

RFIGSHARE_PAT="the-really-long-pat-you-should-have-on-your-clipbord"

保存并关闭文件。确保重新启动 R 会话以使此更改生效。

然后您可以测试一下 运行:

以上是否有效
Sys.getenv("RFIGSHARE_PAT")

查看是否找到您的 PAT。

然后从 sckott 分支安装 rfigshare

remotes::install_github("https://github.com/ropensci/rfigshare/tree/sckott")

现在你应该可以

library(rfigshare)
fs_browse()

我认为其中一个问题是您将 article_id 传递给 fs_browse,第一个参数不是那个。如果您想浏览 public 集,您可以设置 mine = FALSEsession = NULL,例如:

out = fs_details(article_id = 3761562, mine = FALSE, session = NULL)

您还可以考虑利用当前的 figshare api 与 Open API 兼容这一事实,并使用 swagger 规范即时构建您自己的客户端。

按照我在其他回答中的描述生成并存储个人访问令牌。那么你可以做

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)

Figshare 支持人员通知我,他们已阻止使用 http:// 完成的请求。将请求切换到 https:// 似乎解决了 rfigshare 上的一些问题。特别是,fs_details()fs_delete() 在切换到 https:// 后有效。

fs_upload() 即使在切换到 https 后也出现故障。