I am trying to make a FHIR call to a public FHIR server such as SMART Health IT. For that, I have a app that I have hosted using ngrok, and I am launching this app on their server using a custom client_id and client_secret. I am trying to fetch the access and refresh tokens from their /token endpoint using the below code snippet. However, I am getting the error -
{"error":"invalid_request","error_description":"Invalid launch options: TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received undefined"}.
Why is this happening and how can I fix this and make the correct API call?
token_endpoint = "https://launch.smarthealthit.org/v/r4/auth/token"
token_request_data = {
"grant_type": "client_credentials",
"redirect_uri": redirect_uri,
"client_id": client_id,
"client_secret": client_secret,
}
payload = {
"iss": f"{client_id}",
"sub": f"{client_id}",
"aud": f"{token_endpoint}",
"jti": str(uuid.uuid1()),
"exp": time.time() + 60*5
}
jwt_token = jwt.encode(payload, key=client_secret, algorithm="RS384").decode()
grant_type = 'client_credentials'
client_assertion_type = 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer'.replace(':', "%3A")
body = 'grant_type=' + grant_type + '&client_assertion_type=' + client_assertion_type + '&client_assertion=' + jwt_token
token_response = requests.post(token_endpoint, data=body, headers={"Content-Type": "application/x-www-form-urlencoded"})
Note that I am using the requests library only in Python.
Pretty sure the root cause is you're using the wrong base URL of
https://launch.smarthealthit.org/v/r4
, as no CapabilityStatement is defined there. It appears you're trying to use Back-end OAuth, so your token call should be tohttps://launch.smarthealthit.org/v/r4/sim/WzQsIiIsIiIsIiIsMCwwLDAsIiIsIiIsIiIsIiIsIiIsIiIsIiIsMCwxXQ/fhir/auth/token
.While you're at it, a few other things:
jwt.encode()
is handling that automatically but if not, the header needs to be present.token_request_data
object? It doesn't seem to be used anywhere.