如何使用令牌在 R 中调用 API

Ray*_*_90 1 r access-token httr

我正在试验 R,我想知道如何使用库 httr 或 rcurl 从 API 获取一些数据。

例如在终端中使用 curl 我可以做到这一点,它让我得到我想要的数据:

curl -X GET "https://some.webpage.com/api/v1/accounts/self/profile" -H "accept: application/json" -H "token-auth: 72124asfin393483feifefi92835w345"
Run Code Online (Sandbox Code Playgroud)

注意:令牌键是随机字符集

不幸的是,当我尝试在 R 中重现它时我失败了,我尝试使用类似的东西:

> library(httr)
> c <- GET("https://some.webpage.com/api/v1/accounts/self/profile?token- auth=72124asfin393483feifefi92835w345"
Run Code Online (Sandbox Code Playgroud)

或这个:

> url = "https://some.webpage.com/api/v1/accounts/self/profile"
> key = "{72124asfin393483feifefi92835w345}"
> a <- GET(url, add_headers(Authorization = paste("Bearer", key, sep = " ")))
Run Code Online (Sandbox Code Playgroud)

不幸的是,当我在 Rstudio 中尝试此操作时,我总是收到此错误:

[1] "'token-auth' has to be provided for authentication."
Run Code Online (Sandbox Code Playgroud)

有关此特定 API 调用的更多信息:

接口信息

这是文档

所以我想我显然在 url 组合上做错了什么,我如何让它与 R 一起工作?我很困惑,我在 R 中找到了一些关于 API 的文档,但没有解释如何使用令牌。谢谢

Ray*_*_90 5

正确的解决方案是 MrFlick 所说的:

然后尝试 <- GET(url, add_headers("token-auth" = key))。并确保您的密钥字符串中没有大括号。