dreamRs / rte.data

Access data from RTE API
Other
10 stars 5 forks source link

Issue with token base64 encoding #1

Closed cderv closed 5 years ago

cderv commented 5 years ago

There is an issue with token obtention because a \n is added in base64 encoded string with jsonlite. And strangely, this is not handle by the API. This \n addition is on purpose as explained in jeroen/jsonlite#220

Would it be possible to use base64enc or openssl ?

# dummy ids
id_client <- "bcfe5cb5-89ac-4etb-ba60-2f83015c3e78"
id_secret <- "62ade741-453b-4d20-a812-ad1e0aad63ae"
key = list(id_client = id_client, id_secret = id_secret)
raw_string <- charToRaw(paste(key$id_client, key$id_secret, sep = ":"))
jsonlite::base64_enc(raw_string)
#> [1] "YmNmZTVjYjUtODlhYy00ZXRiLWJhNjAtMmY4MzAxNWMzZTc4OjYyYWRlNzQxLTQ1M2ItNGQy\nMC1hODEyLWFkMWUwYWFkNjNhZQ=="
base64enc::base64encode(raw_string)
#> [1] "YmNmZTVjYjUtODlhYy00ZXRiLWJhNjAtMmY4MzAxNWMzZTc4OjYyYWRlNzQxLTQ1M2ItNGQyMC1hODEyLWFkMWUwYWFkNjNhZQ=="
openssl::base64_encode(raw_string)
#> [1] "YmNmZTVjYjUtODlhYy00ZXRiLWJhNjAtMmY4MzAxNWMzZTc4OjYyYWRlNzQxLTQ1M2ItNGQyMC1hODEyLWFkMWUwYWFkNjNhZQ=="

Created on 2019-06-13 by the reprex package (v0.3.0.9000)

key = list(id_client = id_client, id_secret = id_secret)
rte.data::get_token(key)
#> Error: Bad Request (HTTP 400)

get_token2 <- function(key, user = NULL, proxy_pwd = NULL) {
  if (is.list(key))
    key <- base64enc::base64encode(charToRaw(paste(key$id_client, key$id_secret, sep = ":")))
  cli <- crul::HttpClient$new(
    url = rte.data:::get_url("url.auth"),
    headers = list(
      Authorization = paste("Basic", key)
    )
  )
  res <- try(cli$post(), silent = TRUE)
  if ("try-error" %in% class(res))
    stop(proxy_error(getOption("rte.data.url.base")))
  res$raise_for_status()
  txt <- res$parse("UTF-8")
  json <- jsonlite::fromJSON(txt)
  json
}
res <- get_token2(key)
names(res)
#> [1] "access_token" "token_type"   "expires_in"

Created on 2019-06-13 by the reprex package (v0.3.0.9000)

pvictor commented 5 years ago

Hello, Didn't remenber having trouble with tokens, but sure we can use base64enc.

Victor

cderv commented 5 years ago

Something may have change either in jsonlite or in API Server... Not sure where, but your 📦 is working again ! 🎉