我想连接到 COINAPI 资源。它们提供两种类型的授权。https://docs.coinapi.io/#authorization
- 名为 X-CoinAPI-Key 的自定义授权标头
- 查询名为 apikey 的字符串参数
当我使用第一种方法时,它正在处理基本请求。但在更高级的情况下以错误响应。
endpoint<-"/v1/exchangerate/BTC?apikey="
但是当我这样指定端点时:
endpoint <- "/v1/trades/BITSTAMP_SPOT_BTC_USD/history?time_start=2016-01-01T00:00:00/?apikey="
我收到错误 401。
到目前为止,第二种方法不起作用,我不太明白如何在此处指定自定义标头名称。
我需要从这里获取数据:
我将不胜感激有关此问题的任何帮助。
1.方法(工作)
library(httr)
library(jsonlite)
base <- "https://rest.coinapi.io"
endpoint <- "/v1/exchangerate/BTC?apikey="
api_key <- <KEY>
call <- paste0(base, endpoint, api_key)
call
get_prices <- GET(call)
http_status(get_prices)
class(get_prices)
get_prices_text <- content(get_prices, "text", encoding = 'UTF-8')
get_prices_json <- fromJSON(get_prices_text, flatten = TRUE)
names(get_prices_json)
get_prices_json$asset_id_base
head(get_prices_json$rates)
data<-as.data.frame(get_prices_json)
2.方法(不工作)
key<-<KEY>
GET(
url = sprintf("https://rest.coinapi.io/v1/exchangerate/BTC"),
add_headers(`Authorization` = sprintf("X-CoinAPI-Key: ", key))
) -> res
http_status(res)