【发布时间】:2020-05-12 10:11:55
【问题描述】:
我正在学习如何使用 R 中的 API 获取数据。我知道 httr 的目的是为 curl 包提供一个包装器。
为了向 API 发出请求,我正在遵循的文档具有以下 HTTP 请求格式。下面这段代码将用于生成令牌
curl -s \
-d "client_id=clientid” \
-d "username=user” \
-d "password=pwd” \
-d "grant_type=password" \
-d "scope=openid email" \
"https://auth.com/token"
之后,我将使用令牌通过此请求与 API 进行通信
curl --header "Content-Type: application/json" \
--header "Accept: application/+json" \
--header "Authorization: Bearer token_goes_here“ \
--request GET \
--url "https://api-sitename.org/sections?parent_id=0"
最初,我在终端中运行这两个请求,它们都成功了,我得到了 JSON 格式的响应。我的问题是,如何在 R 脚本中运行这些请求,以便我得到响应并且它们是否存储在 R Studio 全局环境中?我的目标是最终将数据集从 API 加载到 Rstudio 工作环境。 T
【问题讨论】:
-
你成功了吗?我有类似的问题,它在终端中工作但在 R 中失败。我传递它的方式有些问题