使用R中的自定义标头和密钥授权COINAPI REST API时出现问题

问题描述 投票:0回答:1

我想连接到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. method (working)

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. method (not working)

key<-<KEY>
GET(
  url = sprintf("https://rest.coinapi.io/v1/exchangerate/BTC"),
  add_headers(`Authorization` = sprintf("X-CoinAPI-Key: ", key))
) -> res
http_status(res)
r rest api httr response.addheader
1个回答
0
投票

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

GET(
  url = sprintf("https://rest.coinapi.io/v1/exchangerate/BTC"),
  add_headers(`X-CoinAPI-Key` = key)
) -> res
http_status(res)
© www.soinside.com 2019 - 2024. All rights reserved.