尝试从API获取数据

时间:2019-08-28 08:19:16

标签: r api-key httr jsonlite

我正在尝试从练习管理软件中获取一些约会数据。我有一个API密钥,但是我在该领域没有经验。

我尝试转换Curl代码,但收效甚微。 api文档在https://github.com/redguava/cliniko-api

我正在尝试转换此卷曲代码

curl https://api.cliniko.com/v1/appointments \
  -u API_KEY: \
  -H 'Accept: application/json' \
  -H 'User-Agent: APP_VENDOR_NAME (APP_VENDOR_EMAIL)'

我尝试过的方法:(是的,这是从curl到r的转换器)

require(httr)

headers = c(
  `Accept` = 'application/json',
  `User-Agent` = 'APP_VENDOR_NAME (APP_VENDOR_EMAIL)'
)

res <- httr::GET(url = 'https://api.cliniko.com/v1/appointments', 
httr::add_headers(.headers=headers), 
httr::authenticate('API_KEY', 'INSERTED MY API KEY'))

任何想法都将不胜感激

1 个答案:

答案 0 :(得分:1)

httr::authenticate采用username形式的输入passwordhttr::authenticate(username,password)

Curl's authenticate接受参数usernamepassword,并由:username:password连接。

在API文档的示例中,curl命令对username:password组合API_KEY:进行身份验证。仔细观察,我们可以看到:之后为空白。由此我们可以确定用户名字段应为“ API_KEY”,密码字段应为“。”。

因此,您应该将curl命令更改为:

require(httr)

headers = c(
  `Accept` = 'application/json',
  `User-Agent` = 'APP_VENDOR_NAME (APP_VENDOR_EMAIL)'
)

res <- httr::GET(url = 'https://api.cliniko.com/v1/appointments', 
httr::add_headers(.headers=headers), 
httr::authenticate('API_KEY', ''))

API_KEY是您提供的API密钥。