我正在尝试让RapidAPI的API R代码工作。
他们提供的代码如下(示意图(:
library(httr)
GET(
url = rapidapi_url,
config = list(x-rapidapi-key = API_KEY,
x-rapidapi-host = HOST_URL,
useQueryString = TRUE),
inboundpartialdate = "2019-12-01"
)
这段代码不起作用,因为(开始时(变量包含一个减号。
我将代码更改为以下格式
#Define Constants
x_rapidapi_key <- xyz # defined higher up in the script for confidentiality reasons and works in rapidapi.com
HTML_link <- "https://yh-finance.p.rapidapi.com/stock/v3/get-historical-data"
YH_file_type = "json"
#' Paste the get_text together
get_text <- paste(HTML_link,
"?q=TTF",
"&x-rapidapi-host:",x_rapidapi_host,
"&x-rapidapi-key:",x_rapidapi_key,
"&out=",YH_file_type,sep="")
httr::GET(get_text)
当运行这个脚本时,我得到了一个401错误,尽管当我在RapidAPI中测试api密钥时,一切都很好。
Response [https://yh-finance.p.rapidapi.com/stock/v3/get-historical-data?q=TTF&x-rapidapi-host:yh-finance.p.rapidapi.com&x-rapidapi-key:xyz&out=json]
Date: 2021-11-08 14:16
Status: 401
Content-Type: application/json
Size: 91 B
我编辑了响应中的api密钥。
请参阅以下相同内容的另一个变体:
library(httr)
url <- "https://yh-finance.p.rapidapi.com/stock/v3/get-historical-data"
queryString <- list(
symbol = "TTF=F",
region = "US"
)
response <- VERB("GET",
url,
add_headers(x_rapidapi_host = 'yh-finance.p.rapidapi.com',
x_rapidapi_key = 'xyz'),
query = queryString,
content_type("application/octet-stream"))
response
content(response, "text")
有人知道怎么做吗?
感谢您的帮助,
Michael
记录:问题已解决:
library(httr)
queryString <- list(
symbol = "TTF=F",
region = "US"
)
res <- GET("https://yh-finance.p.rapidapi.com/stock/v3/get-historical-data",
add_headers(`x-rapidapi-host`='yh-finance.p.rapidapi.com',
`x-rapidapi-key`= '[your API Key]'),
query = queryString)
data <- jsonlite::fromJSON(rawToChar(res$content))
这个秘密似乎是权利"one_answers"的结合。不知道为什么这会有这么大的不同,但它是有效的。
谨致问候,Michael