【问题标题】:Issues with using the API R Code provided by RapidAPI使用 RapidAPI 提供的 API R 代码的问题
【发布时间】:2021-11-08 13:11:55
【问题描述】:

我正在尝试让 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-key 时一切正常。

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-key。

请参阅下面的同一事物的另一个变体:


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

有人知道怎么做吗?

感谢您的帮助,

迈克尔

【问题讨论】:

    标签: r api rapidapi


    【解决方案1】:

    备案:问题已解决:

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

    秘密似乎是正确的`和'的组合。不知道为什么这会产生如此大的不同,但它确实有效。

    问候, 迈克尔

    【讨论】:

      猜你喜欢
      • 2020-03-18
      • 1970-01-01
      • 2021-08-09
      • 1970-01-01
      • 2016-08-12
      • 2020-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多