Struggling with authentication issues when using RapidAPI in R
I’m running into a 401 error every time I attempt to use the RapidAPI service through R. Despite the fact that my API key functions properly on the RapidAPI platform.
Here’s what I’ve done:
library(httr)
# Initial attempt using httr GET with custom headers
api_url <- "https://yh-finance.p.rapidapi.com/stock/v3/get-historical-data"
my_api_key <- "xyz"
host_name <- "yh-finance.p.rapidapi.com"
response <- GET(
url = api_url,
add_headers(
`x-rapidapi-key` = my_api_key,
`x-rapidapi-host` = host_name
),
query = list(
query = "TTF",
region = "US"
)
)
This setup results in a 401 error. I also tried constructing the URL directly:
# Alternative method - constructing the URL manually
base_api_url <- "https://yh-finance.p.rapidapi.com/stock/v3/get-historical-data"
complete_url <- paste0(base_api_url, "?query=TTF®ion=US")
response_manual <- GET(
complete_url,
add_headers(
`x-rapidapi-key` = my_api_key,
`x-rapidapi-host` = host_name
)
)
I’m still receiving the same 401 error. I can confirm that the API key works, as I’ve successfully accessed it via the RapidAPI web interface.
What could I be missing in my R implementation?