Using jsonlite to interact with Spotify API in R - r

I am trying to extract Spotify data via the API
Using the below getAlbums Function:
getAlbums <- function(id,type="album",market="US") {
total <- jsonlite::fromJSON(paste0("https://api.spotify.com/v1/artists/",id,"/albums??&album_type=album"))$total
req <- jsonlite::fromJSON(paste0("https://api.spotify.com/v1/artists/",id,"/albums??offset=0&limit=",total,"&album_type=",type,"&market=",market))
return(req$items[,c("id","name","album_type","available_markets")])
}
I am getting the below error:
Error in open.connection(con, "rb") : HTTP error 400 Extracting Spotify Data
whereas i can successfully Authenticate for other parameters.

Related

HTTR GET request error: Failed writing received data to disk/application

I am trying to send the following API GET Request with the HTTR library in R:
GET('https://api.vitaldb.net/afd182c102c5af625d3f217280b3766d453d9e3f')
But I get the following error message
Error in curl::curl_fetch_memory(url, handle = handle) : Failed writing received data to disk/application
I have tested the specific endpoint in Postman where I am able to retrieve the corret data, but somehow the R command doesn't work.
Can anyone help me?
According to the API docs the file is GZip-compressed CSV format so you could do the following (though someone might know of a one-liner download and read)
library(data.table)
download.file(url = "https://api.vitaldb.net/afd182c102c5af625d3f217280b3766d453d9e3f.csv.gz",
destfile = file.path("data.csv.gz"))
d <- fread("data.csv.gz", sep = ',')

HTTP error 403 when using jsonlite fromJSON in RStudio

library(jsonlite)
url <- "https://api.nftport.xyz/v0/nfts"
data <- fromJSON(url)
a <- as.data.frame(data)
I am new to API, JSON, and R in general. Would like to ask how do I go about building a connection with the API, and retrieving the JSON format, and converting it into a dataframe that I can use.
Have been facing this error.
Error in open.connection(con, "rb") : HTTP error 403.

Scraping RSS XML Feed

I am trying to scrape an RSS feed every hour to run sentiment analysis on headlines of stocks I own. However, I am having issues scraping the rss xml feed. I have tried packages: httr, XML, RCurl and tidyRSS.
I have previously used these packages to successfully scrape xml code, so I am struggling to diagnose the issue. From another answer, I tried dropping the s in https, but no success.
Code with corresponding errors:
my_url <- "https://www.nasdaq.com/feed/rssoutbound?symbol=BAC"
## Failure when receiving data from the peer
feed <- httr::GET(url = my_url)
## Error: 1: failed to load external entity
feed <- XML::xmlTreeParse(my_url, isURL = TRUE)
## error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version
feed <- RCurl::getURL(url = my_url)
## Attempt to fetch feed resulted in an error: Error in curl::curl_fetch_memory(url, handle = handle): Failure when receiving data from the peer
feed <- tidyRSS::tidyfeed(feed = my_url)
I am using R - 4.1.1 with all mentioned packages updated to latest version.

Error in accessing Data using APIs

I can access API data using "curl" on terminal but get error when i use R. Looking for some advice
Curl command:
curl -H "token:jQyrbzexeCEaWFIDBAwCWqbrkrVQTVhM" "https://www.ncdc.noaa.gov/cdo-web/api/v2/datasets"
R command:
books_key <- "&token=jQyrbzexeCEaWFIDBAwCWqbrkrVQTVhM"
url <- "https://www.ncdc.noaa.gov/cdo-web/api/v2/datasets"
req <- fromJSON(paste0(url, books_key))
Error in open.connection(con, "rb") : HTTP error 400.
Usually this error comes when there are spaces in URL (from other similar questions) but in my case there is no space in URL
Some info on using token from website https://www.ncdc.noaa.gov/cdo-web/webservices/v2#gettingStarted
Not a R issue because following example of another website works
movie_key <- "&api-key=b75da00e12d54774a2d362adddcc9bef"
url <- "http://api.nytimes.com/svc/movies/v2/reviews/dvd-picks.json?order=by-date"
req <- fromJSON(paste0(url, movie_key))
You're supposed to pass the token as a header not a query param, e.g. with the crul pkg
cli <- crul::HttpClient$new(
url = "https://www.ncdc.noaa.gov",
headers = list(token = "yourtoken"))
cli$get(path = "cdo-web/api/v2/datasets")
A quick look at your Curl and your R call - in Curl your passing the token as a header variable and in the R call it appears to be a query variable.
Also the NYT call has a query param in the URI so & would be appropriate in the R call.

Error working with jsonlite R package

I want to read in data from an API to R using jsonlite, but gives me the error message shown below. Before some months, it was working fine. What could be the reason that it is not working now? I am trying to get data from the openFDA API:
library(jsonlite)
fda=fromJSON("https://api.fda.gov/drug/event.json?search=receivedate:[20040101+TO+20160101]&count=receivedate")
Error in open.connection(con, "rb") :
Peer certificate cannot be authenticated with given CA certificates

Resources