Authentication information not present ....

I am testing an internal FHIR api, to pull the data into R. This is a test environment with basic HTTP authentication. It works when I try with python http.client.HTTPSConnection . Also works with API tested online. I am not sure what I am missing when using the httr package in R. I get the following message "Authentication information not present or not in the correct format" .
Appreciate your help. I tried the RonFHIR r package and it does not work either.

require("httr")
require("jsonlite")

username <- "userid"
password <- "****"
token<- authenticate(username, password, type = "basic")

url <-"https://test-web-vt02.univ.edu/Interconnect-fhir/api/FHIR/DSTU2/Patient?family=Joe&Given=Mother&Birthdate=2222-12-22", headers=headers"

headers<- httr::add_headers(
	`Content-Type` = "application/json",
	`Authorization` = 'Basic dsdsdsdsdsdsd',
         'epic-client-id': "alphanumericnumber",
        'epic-user-id': "userid",
        'epic-user-idtype': "EXTERNAL")

token_request <- GET(url = url, config = list(headers,token))
token_body <- content(token_request, as = 'parsed')
token_body

python code that works

import http.client
 
conn = http.client.HTTPSConnection("test-web-vt02.univ.edu")
 
headers = {
    'content-type': "application/json",
    'authorization': "Basic *****",
    'epic-client-id': "alphanumericnumber",
    'epic-user-id': "userid",
    'epic-user-idtype': "EXTERNAL"
    }
 
conn.request("GET", "/Interconnect-fhir/api/FHIR/DSTU2/Patient?family=Joe&Given=Mother&Birthdate=2222-12-22", headers=headers)
 
res = conn.getresponse()
data = res.read() 
print(data.decode("utf-8"))

It seems in Python, you don't configure an authentification.
With httr, if you only configure the header with the encoded authorization, do you have the same error ?

1 Like

Yes, I get the same error without configuring an authentication. Also, if I just call the url directly with getURL , I get the error below.

> fromJSON(RCurl::getURL(full_url))
Error in function (type, msg, asError = TRUE)  : 
  SSL certificate problem: unable to get local issuer certificate

There are open servers for SMARTonFHIR and this works,
fromJSON(RCurl::getURL('https://r2.smarthealthit.org/Patient/smart-1551992'))

I am trying to get the basic authentication first, and eventually Oauth2 based connection to FHIR.

If your API use BASIC auth, with httr it should be working with something like this

library(httr)

username <- "userid"
password <- "****"
auth <- authenticate(username, password, type = "basic")
res <- GET(url = url, auth)

If it is an Oauth2 mechanism, httr can deal with it too, but it is different.
You python confirms that basic auth should work.

Tried that, this isn't working. I used httr package in r for other API's and it did work well. This FHIR api is something that is giving an issue. Not sure, why python was able to handle well, where I can get JSON data and unable to replicate in R. httr is literally reading the url, if I paste url in browser I do get "Authentication information not present..." May be something in establishing in connection seems to be different with python and httr or encoding ? I will try to dig more and see if I can find any specific issue.

For now, I am using reticulate package and pulling the JSON object back into R and working with it.

It is difficult to help you more without being able to try myself.

In python, you specify the header directly, this should be the same with httr.

For me you python code would be equivalent to

library(httr)
base_url <- "http://test-web-vt02.univ.edu"

headers <-  add_headers(
  'content-type' = "application/json",
  'authorization' = "Basic *****",
  'epic-client-id' = "alphanumericnumber",
  'epic-user-id' =  "userid",
  'epic-user-idtype' = "EXTERNAL"
)

res <- GET(base_url, 
           path = "/Interconnect-fhir/api/FHIR/DSTU2/Patient",
           query = list(
            family = "Joe",
            Given = "Mother", 
            Birthdate = "2222-12-22"
           ), 
           headers)

data <- content(res) 

I get a new error now for the GET call, I am on windows.

Error in curl::curl_fetch_memory(url, handle = handle) :
Protocol "" not supported or disabled in libcurl

over SO, I found that the curl error is related to SSL certificate. Tried to ignore by the setting below

httr::set_config( config( ssl_verifypeer = 0L ) )

Its a test api and test data, so I shared the original keys on github. But, I understand it is not safe. Its probably too much to ask for, but as I see from your examples and based on the github examples I am passing it as expected by httr. If you would have time to try I can email you the keys.

Did you put the http or https in the url?
In python you didn't put one but I think in R you need to put one.

I don't know if this error is related to SSL.

Also. I think I can't access the url of your internal api.

This worked "https"
base_url <- "https://test-web-vt02.univ.edu"
However, even thought I have specified content-type as JSON, it returns "text/xml"

1 Like

How did you specified content type?
It can also depend of the api.

Thank you for your help. I did not specify separately, just what I passed in the header. 'content-type' = "application/json"

1 Like

To get json. accept_json(). Even though I had Content-Type:Json in headers, I still need to pass this accept_json() to get JSON data. This works.

res <- httr::GET(base_url,
  path = path,
  query = search_query,
  headers,
  accept_json()
)
res

In the header you posted above, you have content-type: application/json. This concerns what you send in the request as body. It is why you don't get a JSON returned. The keyword for that in header is Accept: application/json, to tell the API server that you accept only json in return.

This is exactly what accept_json() helper will add to your current request.
The equivalent of what you are doing currently is httr::content_type_json()

See the help page of those function for more details.

Hope it helps understand what is going on.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.