0

我想连接到 COINAPI 资源。它们提供两种类型的授权。https://docs.coinapi.io/#authorization

  1. 名为 X-CoinAPI-Key 的自定义授权标头
  2. 查询名为 apikey 的字符串参数

当我使用第一种方法时,它正在处理基本请求。但在更高级的情况下以错误响应。

endpoint<-"/v1/exchangerate/BTC?apikey="

但是当我这样指定端点时:

endpoint <- "/v1/trades/BITSTAMP_SPOT_BTC_USD/history?time_start=2016-01-01T00:00:00/?apikey="

我收到错误 401。

到目前为止,第二种方法不起作用,我不太明白如何在此处指定自定义标头名称。

我需要从这里获取数据:

https://rest.coinapi.io/v1/ohlcv/BTC/USD/history?period_id=1DAY&time_start=2017-01-02T00:00:00.0000000Z&time_end=2019-01-02T00:00:00.0000000Z&limit=10000&include_empty_items=TRUE

我将不胜感激有关此问题的任何帮助。

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)
4

1 回答 1

0

通过阅读文档中的示例,它看起来只是在寻找一个简单的标头,而不是专门的“授权”标头。尝试这个

GET(
  url = sprintf("https://rest.coinapi.io/v1/exchangerate/BTC"),
  add_headers(`X-CoinAPI-Key` = key)
) -> res
http_status(res)
于 2019-03-31T23:05:11.813 回答