R - NiceHash API - HMAC signing issue? - r

[This is a duplicate post from NiceHash's GitHub in an attempt to broaden the audience. If I find a solution, at least others will also be able to see it in more places too]
Hi all,
I'm a newbie and the only language I know a little of is R.
I've managed to get a number of other APIs working under R using syntax like this:
# Account Stats
response <- httr::GET(
url = "https://API_URL_GOES_HERE",
)
I think I've got the gist of the syntax for R, but I'm having problems with hashing the Secret Key.
My API key / secret works with the "Try it out" link here.
But I get the usual invalid / 2000 error when passing my hashed secret key via the API.
Here's how I'm trying to generate the hashed secret key:
input <- paste0(APIKey," ", XTime," ", XNonce," "," ", OrgID," ", " ", "GET"," ", "/main/api/v2/accounting/accounts2", " ")
# Needs hashed?
APISecret <- hmac(input, "My_Secret_Key", "sha256")
XAuth <- paste0(APIKey, ":", APISecret)
And here's my API call:
# Account Stats
response<- httr::GET(
url = "https://api2.nicehash.com/main/api/v2/accounting/accounts2",
httr::add_headers(
`X-Time` = XTime,
`X-Nonce` = XNonce,
`X-Organization-Id` = OrgID,
#`X-Request-Id` = XNonce,
`X-Auth`= XAuth,
`Content-Type` = "application/json; charset=utf-8"
)
)
My thoughts are:
input is not correctly formatted
HMAC() isn't generating what NiceHash expects
My GET() isn't structured quite right
Any ideas?
EDIT - Link here explains how the input is supposed to be structured.
EDIT - Link here is examples in other languages, which I can't fully translate to R. The code I've shown above is my attempt.
UPDATE - OK, I'm pretty sure it's something to do with the hashing of the input string.
Using this as input:
4ebd366d-76f4-4400-a3b6-e51515d054d6 ⊠ 1543597115712 ⊠ 9675d0f8-1325-484b-9594-c9d6d3268890 ⊠ ⊠ da41b3bc-3d0b-4226-b7ea-aee73f94a518 ⊠ ⊠ GET ⊠ /main/api/v2/hashpower/orderBook ⊠ algorithm=X16R&page=0&size=100
The following string:
fd8a1652-728b-42fe-82b8-f623e56da8850750f5bf-ce66-4ca7-8b84-93651abc723b
when hashed should lead to:
21e6a16f6eb34ac476d59f969f548b47fffe3fea318d9c99e77fc710d2fed798
But I cannot get that as output. I've no idea what to use in place of ⊠ when forming the input. Other language examples use "\x00", but R just errors with:
Error: nul character not allowed (line 1)

Related

How to deal with German "Umlaute" in an API request?

I am working with the API from the "Deutsche Bahn" for a small private data science project in R. However, cities with an "Umlaut" such as "Köln" or "München" etc. are cumbersome, since the request with the GET command, i.e.,
url_location <- paste0("https://apis.deutschebahn.com/db-api-marketplace/apis/fahrplan/v1/location/","Koeln")
rd <- GET(url_location,
add_headers(Accept = "application/json",
`DB-Client-Id` = "client id",
`DB-Api-Key` = "api key"))
yields entries like K<U+00F6>ln Hbf which I cannot work with.
Question: Is there any option in the GET command (which I could not find) or any alternative which is able to interpret these "Umlaute" appropriately? Or is there a way to substitute these strange "<U+00F6>" parts afterwards?

How to retrieve EPPO Database info via POST?

I can retrieve EPPO DB info from GET requests.
I am looking for help to retrieve the info from POST requests.
Example code and other info in the linked Rmarkdown HTMP output
As suggested, I have gone trough the https://httr.r-lib.org/ site.
Interesting. I followed the links to https://httr.r-lib.org/articles/api-packages.html and then to https://cdn.zapier.com/storage/learn_ebooks/e06a35cfcf092ec6dd22670383d9fd12.pdf.
I suppose that the arguments for the POST() function should be (more or less) as follows, but yet the response is always 404
url = "https://data.eppo.int/api/rest/1.0/"
config = list(authtoken=my_authtoken)
body = list(intext = "Fraxinus anomala|Tobacco ringspot virus|invalide name|Sequoiadendron giganteum")
encode = "json"
#handle = ???
Created on 2021-04-26 by the reprex package (v0.3.0)
How do I find the missing pieces?
It is a little bit tricky:
You need to use correct url with name of the service from https://data.eppo.int/documentation/rest, e.g. to use Search preferred names from EPPOCodes list:
url = "https://data.eppo.int/api/rest/1.0/tools/codes2prefnames"
Authorization should be passed to body:
body = list(authtoken = "yourtoken", intext = "BEMITA")
So, if you want to check names for two eppocodes: XYLEFA and BEMITA the code should look like:
httr::POST(
url = "https://data.eppo.int/api/rest/1.0/tools/codes2prefnames",
body = list(authtoken = "yourtoken", intext = "BEMITA|XYLEFA")
)
Nonetheless, I would also recommend you to just use the pestr package. However, to find eppocodes it uses SQLite database under the hood instead of EPPO REST API. Since the db is not big itself (circa 40MB) this shouldn't be an issue.
I found the easy way following a suggestion in the DataCamp course:
"To find an R client for an API service search 'CRAN '"
I found the 'pestr' package that gives great access to EPPO database.
I still do not know how to use the POST() function myself. Any hint on that side is warmly welcome.
Here is a solution to loop over names to get EPPO-codes. Whit minor adjustments this also works for distribution and other information in the EPPO DB
# vector of species names
host_name <- c("Fraxinus anomala", "Tobacco ringspot virus", "invalide name",
"Sequoiadendron giganteum")
EPPO_key <- "enter personal key"
# EPPO url
path_eppo_code <- "https://data.eppo.int/api/rest/1.0/tools/names2codes"
# epty list
my_list <- list()
for (i in host_name) {
# POST request on eppo database
response <- httr::POST(path_eppo_code, body=list(authtoken=EPPO_key,
intext=i))
# get EPPO code
pest_eppo_code <- strsplit(httr::content(response)[[1]], ";")[[1]][2]
# add to list
my_list[[i]] <- pest_eppo_code
}
# list to data frame
data <- plyr::ldply(my_list)

Authorization in R API, specifically for League of Legends

I am learning how to use API in R and it is going well for the most part, but I am having trouble getting any data from the league of legends API.
For reference, I used this article as a start (https://www.dataquest.io/blog/r-api-tutorial/) and cop
res <- GET("http://api.open-notify.org/astros.json")
res
This worked just fine and has a 200 status, but I am not interested in that data.
What I want is data about league of legends, so I am trying to use:
base.url <- "https://na1.api.riotgames.com"
path <- "/lol/champion-mastery/v4/champion-masteries/by-summoner/"
API_Key <- read.table("riotkey.txt")
API_KEY <- API_Key$V1
Summoner_ID <- read.table("summonerID.txt")
SUMMONER_ID <- Summoner_ID$V1
path <- paste0(path,SUMMONER_ID)
LoL_API_Test <- GET(base.url, path = path,
add_headers(Authorization = API_KEY))
LoL_API_Test
This is Riot's explanation for the 403 error - Forbidden. "This error indicates that the server understood the request but refuses to authorize it. There is no distinction made between an invalid path or invalid authorization credentials (e.g., an API key)"
I am certain that my API key and summoner ID are correct.
So I assume the issue has to be with how I am requesting the data.
What am I doing wrong?
This particular API expects the API key to be passed in a header called "X-Riot-Token", not "Authorization". Change your call to
LoL_API_Test <- GET(base.url, path = path,
add_headers("X-Riot-Token" = API_KEY))

Using HTTR POST to Create a Azure Devops Work Item

Im attempting to setup some R code to create a new work item task in Azure Devops. Im okay with a mostly empty work item to start with if thats okay to do (my example code is only trying to create a work item with a title).
I receive a 203 response but the work item doesn't appear in Devops.
Ive been following this documentation from Microsoft, I suspect that I might be formatting the body incorrectly.
https://learn.microsoft.com/en-us/rest/api/azure/devops/wit/work%20items/create?view=azure-devops-rest-5.1
Ive tried updating different fields and formatting the body differently with no success. I have attempted to create either a bug or feature work item but both return the same 203 response.
To validate that my token is working I can GET work item data by ID but the POST continues to return a 203.
require(httr)
require(jsonlite)
url <- 'https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/$bug?api-version=5.1'
headers = c(
'Authorization' = sprintf('basic %s',token),
'Content-Type' = 'application/json-patch+json',
'Host' = 'dev.azure.com'
)
data <- toJSON(list('body'= list("op"= "add",
"path"= "/fields/System.AreaPath",
"value"= "Sample task")), auto_unbox = TRUE, pretty = TRUE)
res <- httr::POST(url,
httr::add_headers(.headers=headers),
httr::verbose(),
body = data)
Im expecting a 200 response (similar to the example in the link above) and a work item task in Azure DevOps Services when I navigate to the website.
Im not the best with R so please be detailed. Thank you in advanced!
The POST continues to return a 203.
The HTTP response code 203 means Non-Authoritative Information, it should caused by your token format is converted incorrectly.
If you wish to provide the personal access token through an HTTP
header, you must first convert it to a Base64 string.
Refer to this doc described, if you want to use VSTS rest api, you must convert your token to a Base64 string. But in your script, you did not have this script to achieve this convert.
So, please try with the follow script to convert the token to make the key conformant with the requirements(load the base64enc package first):
require(base64enc)
key <- token
keys <- charToRaw(paste0(key,":token"))
auth <- paste0("Basic ",base64encode(keys))
Hope this help you get 200 response code
I know this question is fairly old, but I cannot seem to find a good solution posted yet. So, I will add my solution in case others find themselves in this situation. Note, this did take some reading through other SO posts and trial-and-error.
Mengdi is correct that you do need to convert your token to a Base64 string.
Additionally, Daniel from this SO question pointed out that:
In my experience with doing this via other similar mechanisms, you have to include a leading colon on the PAT, before base64 encoding.
Mengdi came up big in another SO solution
Please try with adding [{ }] outside your request body.
From there, I just made slight modifications to your headers and data objects. Removed 'body' from your json, and made use of paste to add square brackets as well. I found that the Rcurl package made base64 encoding a breeze. Then I was able to successfully create a blank ticket (just titled) using the API! Hope this helps someone!
library(httr)
library(jsonlite)
library(RCurl)
#user and PAT for api
userid <- ''
token= 'whateveryourtokenis'
url <- 'https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/$bug?api-version=5.1'
#create a combined user/pat
#user id can actually be a blank string
#RCurl's base64 seemed to work well
c_id <- RCurl::base64(txt = paste0(userid,
":",
devops_pat
),
mode = "character"
)
#headers for api call
headers <- c(
"Authorization" = paste("Basic",
c_id,
sep = " "
),
'Content-Type' = 'application/json-patch+json',
'Host' = 'dev.azure.com'
)
#body
data <- paste0("[",
toJSON(list( "op"= "add",
"path"= "/fields/System.Title",
"value"= "API test - please ignore"),
auto_unbox = TRUE,
pretty = TRUE
),
"]"
)
#make the call
res <- httr::POST(url,
httr::add_headers(.headers=headers),
httr::verbose(),
body = data
)
#check status
status <- res$status_code
#check content of response
check <- content(res)

How to use httr correctly for X-WSSE Authentification?

I´m trying to connect to the API of emarsys. They use X-WSSE as authentification method and i´m stuck and need to figure out what i am doing wrong. I tried to make the header as requested, but i don´t know where it went wrong. I´m very thankful for your comments!
install.packages("httr")
install.packages("digest")
library("httr")
library("digest")
# prepare userdata
username <- "customer001"
secretkey <- "supersecretkey"
timestamp <- format(as.POSIXlt(Sys.time(), "UTC"), "%Y-%m-%dT%H:%M:%SZ")
nonce <- digest(random(8), length=16)
# passworddigest
pwd <- paste0(nonce, timestamp, secretkey)
pwd <- digest::sha1(pwd, algo="sha1", serialize=FALSE)
pwd <- jsonlite::base64_enc(charToRaw(pwd))
URL_base <- "https://api.emarsys.net/api/v2/"
URL_endpoint <- "contact/settings"
URL <- paste0(URL_base,URL_endpoint)
# create header
header <- c(paste0('UsernameToken ',
'Username="', username, '", ',
'PasswordDigest="', pwd,'", ',
'Nonce="', nonce, '", ',
'Created="', timestamp,'"'))
# name header
names(header) <- 'X-WSSE:'
# make httr request
response <- GET(URL, add_headers(.headers = header))
response
The http-header should look like this:
X-WSSE: UsernameToken Username="customer001",
PasswordDigest="ZmI2ZmQ0MDIxYmQwNjcxNDkxY2RjNDNiMWExNjFkZA==",
Nonce="d36e3162829ed4c89851497a717f", Created="2014-03-20T12:51:45Z"
But i don´t know where i can find out, how the request from my httr-code looks and what i do different.
EDIT:
"digest::sha1(pwd, algo="sha1", serialize=FALSE)" has to be digest(pwd, algo="sha1", serialize=FALSE). Then it works.
This is kind of a comment but I can't get formatting the way it needs to be there for clarity so here it is. SO pedants can feel free to downvote this if so moved:
First, add a verbose() parameter to the GET() call to see if what you're passing is what you think it should be. I'm betting the issue is the : in the 'X-WSSEP:' value you are assigning. I also find it much easier to use named parameters to the add_headers() call so perhaps try:
GET(
url = URL,
add_headers(`X-WSSE` = header),
verbose()
)
to see if that clears this up or at least gets you a bit further.
Also: once your issues are solved and you get the access you want, consider making a pkg for the Emarsys API. If you've not made packages before it cld be a ++gd learning experience and either way it may help others.

Resources