I am trying to access a RapidAPI with the httr package in R as follows:
library(httr)
url <- "https://extract-news.p.rapidapi.com/v0/article"
queryString <- list(url = "https://www.theverge.com/2020/4/17/21224728/bill-gates-coronavirus-lies-5g-covid-19")
response <- VERB("GET", url, addheaders(x_rapidapi_key ="my-api-key", x_rapidapi_host = "extract-news.p.rapidapi.com"), query = queryString, contenttype("application/octet-stream"))
content(response, "text")
I tried accessing other APIs too. However, I keep getting this error message:
Status: 401
Could you please help me solve this issue?
Thanks a lot in advance!
Try using content_type() and add_headers instead of contenttype and addheaders to tell the server what sort of data you are sending. Read more about the VERB method here.
library(httr)
url <- "https://extract-news.p.rapidapi.com/v0/article"
queryString <- list(url = "https://www.theverge.com/2020/4/17/21224728/bill-gates-coronavirus-lies-5g-covid-19")
response <- VERB("GET", url, add_headers(x_rapidapi-host = 'extract-news.p.rapidapi.com', x_rapidapi-key = '*************************', '), query = queryString, content_type("application/octet-stream"))
content(response, "text")
Related
Using a browser, I can navigate to https://myaccount.draftkings.com/login and login using a username and password. Then if I navigate to https://api.draftkings.com/scores/v1/leaderboards/9000?format=json&embed=leaderboard in another tab, I can see the API response.
I want to do this programmatically.
There is a python package that I previously used successfully for this purpose. But for some reason, I can't get that package to work now.
I believe the python package hits an API endpoint with a post request to authenticate and saves the cookies to a file that are later used to make requests.
I tried sending a post request to the login endpoint the python package uses with httr2:
req <- httr2::request("https://api.draftkings.com/users/v3/providers/draftkings/logins?format=json") %>%
httr2::req_method("POST") %>%
httr2::req_headers(
"Contest-Type" = "application/json",
"Accept" = "*/*",
"Accept-Encoding" = "gzip, deflate, br"
) %>%
httr2::req_body_json(
list(
"login" = "email",
"password" = "password",
"host" = "api.draftkings.com",
"challengeResponse" = list("solution" = "", "type" = "Recaptcha")
)
)
req %>%
httr2::req_perform()
but I get a 403 error.
I also tried logging in using rvest:
library(rvest)
url <- "myaccount.draftkings.com/login"
session <- session(url)
form <- session %>%
html_form() %>%
magrittr::extract2(1)
form$action <- url
filled_form <- form %>%
html_form_set(!!! list(EmailOrUsername = "user",
Password = 'password'))
html_form_submit(filled_form, submit = 3)
session_jump_to(session, "https://www.draftkings.com/lobby")
but that didn't seem to do what I want either. Note that the form object returned an empty action element. I'm not sure what to replace the action element with, but if I leave it null I get an error.
I also tried saving the cookies from a browser session after logging into draftkings.com and passing all the cookies to a GET request with httr:
cook <- jsonlite::read_json("cookies.json")
clean_cook <- unlist(lapply(cook, function(x) {stats::setNames(x$value, x$name)}))
resp <- httr::GET(
"https://api.draftkings.com/scores/v1/leaderboards/9000?format=json&embed=leaderboard",
httr::set_cookies(.cookies = clean_cook)
)
httr::content(resp)
but this returns a 400 error code and message "Invalid userKey.". This is the same error message I get if I clear my cache in the browser and then visit https://api.draftkings.com/scores/v1/leaderboards/9000?format=json&embed=leaderboard. I don't think the issue is related to URL encoding of the cache values. I tried restarting my RStudio session.
Update
I figured out how to successfully perform the GET request using the cookies saved from my browser and httr2:
cook <- jsonlite::read_json("cookies.json")
clean_cook <- paste0(unlist(lapply(cook, function(x) {paste0(x$name, "=", x$value)})), collapse = ";")
req <- httr2::request(
"https://api.draftkings.com/scores/v1/leaderboards/9000?format=json&embed=leaderboard"
)
req <- httr2::req_headers(req, "cookie" = clean_cook)
resp <- httr2::req_perform(req)
str(httr2::resp_body_json(resp))
For some reason, I was unable to get httr::set_cookies() to work. Passing cookies in the header (using httr::add_headers) was also not reliably successful using httr.
I am trying to use httr and the code snippet from rapidapi.com to use sky scanner API.This is the first time I am trying this.
My issue is that the code copied directly from the site is not working and this is because of a ' in the code.
How can I debug this error so that I can use the 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")
Edit-1
I found a post on the site here that explained that the site gives 2 errors in the code snippet and suggests altering the code. This is giving a different error however. I cannot correctly input the response object.
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")
I'm not sure if this is the right response for this, but your first code snippet has the extra ' at the end like you said:
config = httr::add_headers(x_rapidapi-key = API_KEY , x_rapidapi-host = HOST_URL,'),
try changing to
config = httr::add_headers(x_rapidapi-key = API_KEY , x_rapidapi-host = HOST_URL),
Altogether I'd try :
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")))
Then check the response by just response
I got a solution to the problem. This should correct the code snippet and let it run in 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
This corrects the exact code on rapid API's snippet.
Always use the code snippet that RapidAPI provides. It's authentic and always works. They provide support for 20 programming languages with 40 different libraries.
Try this code snippet:
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")
I am trying to get data from an api for a data science project. The code below is the GET request that I used and it's giving me an error. Please let me know how to fix this error.
This is the code I wrote:
library(httr)
url <- "https://netflix-unofficial.p.rapidapi.com/api/search"
queryString <- list(year = "2020")
response <- VERB("GET", url, add_headers(x_rapidapi-key = 'MY_KEY', x_rapidapi-host = 'netflix-unofficial.p.rapidapi.com', '), query = queryString, content_type("application/octet-stream"))
content(response, "text")
this is the error I am getting:
Error: unexpected '=' in "response <- VERB("GET", url, add_headers(x_rapidapi-key ="
I have a question about to add body to GET request.
In Postman, I easily add body raw json to tab Body
In R, I use httr::GETand I can not find any option to add them. I try to use option query, but it return error 500 "Missing parameter username"
This is my code (sorry about fake data):
library(httr)
api <- ".........................../users/authorize"
query <- list("username": "xxxxx", "password": "xxxxxxxxxxxx")
resp <- GET(api, query = query)
stop_for_status(resp)
# Error: Internal Server Error (HTTP 500).
content(resp, type = "text", encoding = "utf-8")
# [1] "{\"status\":\"0\",\"error_code\":\"S002\",\"errors\":\"Missing param [username].\"}"
Can anybody help me to this case? Thank you so much.
Try this:
resp <- httr::POST(body = query, encode = "json")
I am having trouble with DELETE requests in R. I have been successful in making GET and POST requests using the below code. Any help / pointers will be appreciated.
It will require an api.key, secret & passphrase from GDAX to work.
Here is my function:
library(RCurl)
library(jsonlite)
library(httr)
library(digest)
cancel_order <- function(api.key,
secret,
passphrase) {
api.url <- "https://api.gdax.com"
#get url extension----
req.url <- "/orders/"
#define method----
method = "DELETE"
url <- paste0(api.url, req.url)
timestamp <-
format(as.numeric(Sys.time()), digits = 13) # create nonce
key <- base64Decode(secret, mode = "raw") # encode api secret
#create final end point----
what <- paste0(timestamp, method, req.url)
#create encoded signature----
sign <-
base64Encode(hmac(key, what, algo = "sha256", raw = TRUE)) # hash
#define headers----
httpheader <- list(
'CB-ACCESS-KEY' = api.key,
'CB-ACCESS-SIGN' = sign,
'CB-ACCESS-TIMESTAMP' = timestamp,
'CB-ACCESS-PASSPHRASE' = passphrase,
'Content-Type' = 'application/json'
)
##------------------------------------------------
response <- getURL(
url = url,
curl = getCurlHandle(useragent = "R"),
httpheader = httpheader
)
print(rawToChar(response)) #rawToChar only on macOS and not on Win
}
The error I get is "{\"message\":\"invalid signature\"}" even though the same command will code and signature will work with GET & POST.
Ref: GDAX API DOCs
just a guess as I am not familiar with the API, but perhaps you are missing the 'order-id' ...
look at: https://docs.gdax.com/?javascript#cancel-an-order
Ok. I took #mrflick's advise and pointed my connection to requestbin based on his feedback on a different but related question.
After careful inspection, I realized that the my request for some reason was treated as a POST request and not a DELETE request. So I decided to replace the getURL function with another higher level function from RCurl for it to work.
response <- httpDELETE(
url = url,
curl = getCurlHandle(useragent = "R"),
httpheader = httpheader
)
Everything else remains the same. Apparently there never was an issue with the signature.
I have added this function now to my unofficial wrapper rgdax
EDIT::
The unofficial wrapper is now official and on CRAN.