通过 httr 使用 RapidAPI 的 SkyScanner 集成

Use RapidAPI's SkyScanner integration using httr

我正在尝试使用 httr 和来自 rapidapi.com 的代码片段来使用天空扫描仪 API.This 这是我第一次尝试这个。

我的问题是直接从站点复制的代码无法正常工作,这是因为代码中的 '

如何调试此错误以便我可以使用 API?

library(httr)

url0 <- "https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/reference/v1.0/currencies"
API_KEY <- 'my_API_key'
HOST_URL <- 'skyscanner-skyscanner-flight-search-v1.p.rapidapi.com'
response <- VERB(verb="GET",
                  url=url0, 
                 config = httr::add_headers(x_rapidapi-key = API_KEY , x_rapidapi-host = HOST_URL,'),
                 encode = content_type("application/octet-stream"))

content(response, "text")

编辑-1

我在此处的网站上发现了一个 post,它解释说该网站在代码片段中给出了 2 个错误并建议 altering the code。然而,这给出了不同的错误。我无法正确输入 response 对象。

library(httr)

url <- "https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/reference/v1.0/currencies"
API_KEY <-  'my_API_key'

response <- VERB("GET",
                 url,
                 add_headers(x-rapidapi-key = API_KEY,
                             x-rapidapi-host = "skyscanner-skyscanner-flight-search-v1.p.rapidapi.com"),
                 content_type("application/octet-stream"))

content(response,"text")

我不确定这是否是对此的正确回应,但你的第一个代码片段末尾有额外的 ' 就像你说的那样:

config = httr::add_headers(x_rapidapi-key = API_KEY , x_rapidapi-host = HOST_URL,'),

尝试更改为

config = httr::add_headers(x_rapidapi-key = API_KEY , x_rapidapi-host = HOST_URL),

总而言之,我会尝试:

library(httr)

url0 <- "https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/reference/v1.0/currencies"
API_KEY <- 'my_API_key'
HOST_URL <- 'skyscanner-skyscanner-flight-search-v1.p.rapidapi.com'
response <- VERB(verb="GET",
                  url=url0, 
                 config = httr::add_headers(x_rapidapi-key = API_KEY, x_rapidapi-host = HOST_URL, content_type("application/octet-stream")))

然后通过 response

检查响应

我找到了解决问题的方法。这应该更正代码片段并让它在 R.

中 运行
# Correct
library(httr)

url <- "https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/reference/v1.0/currencies"
API_KEY <- "your_key"

response <- VERB("GET",
                 url,
                 add_headers("x-rapidapi-key" = API_KEY,
                             "x-rapidapi-host" = "skyscanner-skyscanner-flight-search-v1.p.rapidapi.com"),
                 content_type("application/octet-stream"))

content(response,"text")

Robject <- content(response, "text")
Robject

这更正了 rapid API 代码段中的确切代码。

始终使用 RapidAPI 提供的代码片段。它是真实的,并且始终有效。他们为 20 种编程语言和 40 个不同的库提供支持。

试试这个代码片段:

    library(httr)
    url <- "https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/reference/v1.0/currencies"
    
    response <- VERB("GET", url, add_headers(x_rapidapi-host = 'skyscanner-skyscanner-flight-search-v1.p.rapidapi.com', x_rapidapi-key = '*****************************', '), content_type("application/octet-stream"))
    
    content(response, "text")