I tried to do a API call to get data from a website called BirdEye. They have a developer website with instructions on API calls. However, I am having trouble converting curl code into R code using httr package.
Example:
curl -H "content-type: application/json" -H "accept:application/json" -X POST -d { "fromDate":"04/01/2013", "toDate":"05/3/2015", "updateFromDate":"04/01/2013", "updateToDate":"05/3/2015", "sources":["google","citysearch"], "ratings":[1,2,3,4,5,0],"searchStr":"Awesome","subBusinessIds":[145308764185002,145308769721320,145308702026610],"tags":["tag_1","tag_2"] } "https://api.birdeye.com/resources/v1/review/businessId/755009344_1?sindex=0&count=25&api_key=92bcd6e0-c102-43fd-8a67-1a7be5258451" -v
Here is what I did:
headers <- c('Accept' = 'application/json', 'Content-Type' = 'application/json')
body_content <- list('fromDate' = '2019-01-01', 'toDate' = '2019-05-01', 'sources' = 'google', 'ratings' = c(1, 2, 3, 4, 5, 0))
resp <- POST(url ="https://api.birdeye.com/resources/v1/review/businessId/xxxxxxxxxx?sindex=0&count=20&api_key=xxxxxxxxxxxxxxxxxxxxxxxxxxx",
body = body_content, content_type("application/json"), accept("application/json"))
But I got an error message below:
Unexpected character ('-' (code 45)) in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value
Warning message:
In charToRaw(enc2utf8(val)) :
argument should be a character vector of length 1
all but the first element will be ignored
Could someone help with the issue? Appreciate it!
Related
I am trying to get data from Eikon Elektron cloud platform.
I ran following codes from https://github.com/Refinitiv/websocket-api/blob/master/Applications/Examples/R/market_price_authentication.R:
library(curl)
> content = paste("grant_type=", "password","&username=", user, "&password=", password, sep="")
> h <- new_handle(copypostfields = content)
> h
<curl handle> (empty)
> handle_setheaders(h,
+ "Content-Type" = "application/x-www-form-urlencoded"
+ )
> handle_setopt(h, ssl_verifypeer = FALSE, ssl_verifyhost = FALSE)
> auth_url = paste("https://", auth_hostname, sep="")# ":", auth_port, "/getToken", sep="")
> auth_url
[1] "https://api.refinitiv.com/auth/oauth2/v1/token"
> req <- curl_fetch_memory(auth_url, **handle = h**)
> req
$url
[1] "https://api.refinitiv.com/auth/oauth2/v1/token"
$status_code
[1] 400
$type
[1] "application/json"
**> h
<curl handle> (https://api.refinitiv.com/auth/oauth2/v1/token)**
> res_headers = parse_headers(req$headers)
> auth_json_string = rawToChar(req$content)
> auth_json = fromJSON(auth_json_string)
> cat(toJSON(auth_json, pretty=TRUE, auto_unbox=TRUE))
{
"error": "invalid_request"
}
As you can see, I got invalid request error. I think the problem lies in curl_fetch_memory and that the handle=h is using same input as auth_url, however it should use something similiar to the input of content. What can I change in my code to make it work?
I found solution how to access the URL. In github was wrongly written that app_id in R file should equal to the code from the App Key generator in Reuters. However, app_id and client_id are different things and you should add client_id=value from App Key generator (not app_id).+ also do not forget to include trapi and etc.. to your content.
I want to call Amazon product advertising API from R. Given below is the quick guide of pa API 5.
https://webservices.amazon.com/paapi5/documentation/quick-start/using-curl.html
I tried to do the way it described here https://webservices.amazon.com/paapi5/documentation/sending-request.html using 'httr' but got thrown off on the signature version 4 signing process.
I tried using 'aws.signature' package for signature prior to calling the POST function, but the final output I am getting is status code 500.
Here is the code I have used
library(httr)
library(jsonlite)
library(aws.signature)
request_body=data.frame("Keywords"="Harry",
"Marketplace"= "www.amazon.com",
"PartnerTag"= "mytag-20",
"PartnerType"= "Associates",
"Access Key"="my_accesskey",
"Secret Key"="my_secret_key",
"service"="ProductAdvertisingAPIv1",
"Region"="us-east-1"
"Resources"="Offers.Listings.Price",
"SearchIndex"= "All")
request_body_json=toJSON(request_body,auto_unbox=T)
request_body_json=gsub("\\[|\\]","",request_body_json)
t=signature_v4_auth(
datetime = format(Sys.time(), "%Y%m%dT%H%M%SZ", tz = "UTC"),
region = NULL,
service="ProductAdvertisingAPIv1",
verb="POST",
"com.amazon.paapi5.v1.ProductAdvertisingAPIv1.SearchItems",
query_args = list(),
canonical_headers=c("Host: webservices.amazon.com",
"Content-Type: application/json; charset=UTF-8",
"X-Amz-Target: com.amazon.paapi5.v1.ProductAdvertisingAPIv1.SearchItems",
"Content-Encoding: amz-1.0",
"User-Agent: paapi-docs-curl/1.0.0"),
request_body=request_body_json,
signed_body = TRUE,
key = "access_key",
secret = "secret-key",
session_token = NULL,
query = FALSE,
algorithm = "AWS4-HMAC-SHA256",
force_credentials = FALSE,
verbose = getOption("verbose", FALSE)
)
result=POST("https://webservices.amazon.com/paapi5/searchitems",body=request_body_json,
add_headers(.headers=c("Host: webservices.amazon.com",
"Content-Type: application/json; charset=UTF-8",
paste("X-Amz-Date:",format(Sys.time(), "%Y%m%dT%H%M%SZ", tz = "UTC")),
"X-Amz-Target: com.amazon.paapi5.v1.ProductAdvertisingAPIv1.SearchItems",
"Content-Encoding: amz-1.0",
"User-Agent: paapi-docs-curl/1.0.0",
paste0("Authorization: AWS4-HMAC-SHA256 Credential=",t[["Credential"]],"SignedHeaders=content-encoding;host;x-amz-date;x-amz-target Signature=",t[["Signature"]])
)))
Appreciate it if anyone one can help with this. Thanks
I have curl request that is working file, which is as follows:
curl -XGET "https://xxxx.com/xxx" -u "username:password"
How I want to do it using curl package in R
I have following code,
library(curl)
clCall <- new_handle(url = 'https://xxxx.com/xxx')
handle_setopt(clCall, customrequest = "XGET")
Now I don't know how to pass username and password in this curl request
You should set httpauth and userpwd options too:
h <- curl::new_handle()
curl::handle_setopt(
handle = h,
httpauth = 1,
userpwd = "user:passwd"
)
resp <- curl::curl_fetch_memory("https://httpbin.org/basic-auth/user/passwd", handle = h)
jsonlite::fromJSON(rawToChar(resp$content))
#> $authenticated
#> [1] TRUE
#>
#> $user
#> [1] "user"
I'm following the official manual of opencpu package in R. In chapter 4.3 Calling a function It uses curl to test API:
curl http://your.server.com/ocpu/library/stats/R/rnorm -d "n=10&mean=100"
and the sample output is:
/ocpu/tmp/x032a8fee/R/.val
/ocpu/tmp/x032a8fee/stdout
/ocpu/tmp/x032a8fee/source
/ocpu/tmp/x032a8fee/console
/ocpu/tmp/x032a8fee/info
I can use curl to get similar result, but when I try to send this http request using httr package in R, I don't know how to replicate the result. Here is what I tried:
resp <- POST(
url = "localhost/ocpu/library/stats/R/rnorm",
body= "n=10&mean=100"
)
resp
the output is:
Response [HTTP://localhost/ocpu/library/stats/R/rnorm]
Date: 2015-10-16 00:51
Status: 400
Content-Type: text/plain; charset=utf-8
Size: 30 B
No Content-Type header found.
I guess I don't understand what's the equivalence of curl -d parameter in httr, how can I get it correct?
Try this :)
library(httr)
library(jsonlite)
getFunctionEndPoint <- function(url, format) {
return(paste(url, format, sep = '/'))
}
resp <- POST(
url = getFunctionEndPoint(
url = "https://public.opencpu.org/ocpu/library/stats/R/rnorm",
format = "json"),
body = list(n = 10, mean = 100),
encode = 'json')
fromJSON(rawToChar(resp$content))
I want to do
curl -H "Authorization: Basic YOUR_API_KEY" -d '{"classifier_id":155, "value":"TEST"}' "https://www.machinelearningsite.com/language/classify"
I tried
h = getCurlHandle(header = TRUE, userpwd = YOUR_API_KEY, netrc = TRUE)
out <- getURL("https://www.machinelearningsite.com/language/classify?classifier_id=155&value=TEST", curl=h,ssl.verifypeer=FALSE)
but it says method not allowed
It's much easier to translate curl command-line arguments into httr calls:
library(httr)
result <- GET("https://www.machinelearningsite.com/language/classify",
add_headers(Authorization=sprintf("Basic %s", YOUR_API_KEY),
query=list(classifier_id=155, value="TEST")))
ideally, YOUR_API_KEY would be an environment variable, so you can change that to:
result <- GET("https://www.machinelearningsite.com/language/classify",
add_headers(Authorization=sprintf("Basic %s", Sys.getenv("YOUR_API_KEY")),
query=list(classifier_id=155, value="TEST")))
You can then do:
content(result)
To retrieve the actual data.