HTTP error 403 when using jsonlite fromJSON in RStudio - r

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.

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 = ',')

Error while using rjson package to retrive Yahoo's API Stock data

I'm trying to get SPY prices from Yahoo real-time API, the code used to work in the past but now I get this error (below), How can I overcome this problem
library(rjson)
json_file <- "http://finance.yahoo.com/webservice/v1/symbols/SPY/quote?format=json&view=detail"
json_data <- fromJSON(paste(readLines(json_file), collapse=""))
json_data
I get this error:
"Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
cannot open URL 'http://finance.yahoo.com/webservice/v1/symbols/AA/quote?format=json&view=detail': HTTP status was '406 Not Acceptable'
You can use this URL as an alternative. It downloads a CSV format but can be parsed easily. http://download.finance.yahoo.com/d/quotes.csv?s=GOOG+APPL&f=nl1r

R jsonlite - fromJSON always returns Error in open.connection?

Why do I always get the connection error with fromJSON? Sometimes it is fine. Most of time it is not.
> # Load json and other packages.
> library(jsonlite)
>
> # Live server.
> server <- 'http://0.0.0.0:3000'
> # Stream 143
> key <- '9p06nngO2pcQM03nIJ71dLXNA1v'
>
> # Retrieve json data from the data platform via the URLs.
> streams <- fromJSON(paste(server, '/output/streams', sep=""), flatten=TRUE)
Result:
Error in open.connection(con, "rb") : Couldn't connect to server
If I try again:
> streams <- fromJSON(paste(server, '/output/streams', sep=""), flatten=TRUE)
Error:
Error in open.connection(con, "rb") : Server returned nothing (no
headers, no data)
But the server is fine. It is up and running.
Any ideas how I can fix this?
Or any other better and more reliable package for getting the json data from the server?
It seems to me that there is some special character in the public_key field. For instance:
Gurl<-"http://139.162.208.52:3000/output/stream?public_key=9p06nngO2pcQM03nIJ71dLXNA1v"
Burl<-"http://139.162.208.52:3000/output/stream?public_key=9p06nngO‌​2pcQM03nIJ71dLXNA1v"
#this seems to work
fromJSON(Gurl, flatten=TRUE)
#this doesn't
fromJSON(Burl, flatten=TRUE)
Burl and Gurl are only apparently the same:
Gurl==Burl
#[1] FALSE
You can inspect the raw content of the above urls (with charToRaw) and check the Encoding to spot the differences.
Eg. Php, basic just get data from server by json_encode
What programming languages are u using? For back-end

Using jsonlite to interact with Spotify API in 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.

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