0

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

我的问题是直接从网站复制的代码不起作用,这是因为 ' 代码中有 a。

如何调试此错误以便我可以使用 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

我在网站上发现了一篇文章,解释说该网站在代码片段中给出了 2 个错误,并建议更改代码。然而,这给出了一个不同的错误。我无法正确输入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")

4

3 回答 3

1

我找到了解决问题的办法。这应该更正代码片段并让它在 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的代码片段上的确切代码。

于 2021-05-24T21:18:44.127 回答
1

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

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

于 2021-05-24T21:11:22.327 回答
0

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

试试这个代码片段:

    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")
于 2021-09-15T09:48:10.593 回答