You are here: Jython Script Interfaces > Utility functions: HTTP with OAuth

Jython HTTP Client Code Samples for OAuth

 

 

(For HTTP without OAuth, see this page.)

 

These code samples need a server which implements OAuth in order for them to work. At the time of writing, MD Link doesn't include an OAuth server, and we don't know of any public test servers which are appropriate for this. Therefore these samples will require substantial additional setup.

 

OAuth type: client credentials / JWT 

 

The code sample below is adapted from working code which creates a FHIR patient resource on the EPIC FHIR Sandbox. If you want to run this code, one way is to:

 

requestBody = """{

"resourceType": "Patient",

"identifier": [ { "use": "usual", "system": "urn:oid:2.16.840.1.113883.4.1", "value": "484-76-1115" } ],

"active": true,

"name": [ { "family": "Chalmers", "given": [ "Peter", "Daniel" ] } ],

"gender": "male",

"birthDate": "1976-12-25",

"_birthDate": { "extension": [ { "url": "http://hl7.org/fhir/StructureDefinition/patient-birthTime", "valueDateTime": "1974-12-25T14:35:45-05:00" } ] },

"deceasedBoolean": false,

"address": [ { "type": "both", "line": [ "511 pm Erewhon St" ], "city": "PleasantVille", "postalCode": "3999", "period": { "start": "1974-12-25" } } ],

"contact": [ { "relationship": [ { "coding": [ { "system": "http://terminology.hl7.org/CodeSystem/v2-0131", "code": "N" } ] } ], "name": { "family": "du march", "_family": { "extension": [ { "url": "http://hl7.org/fhir/StructureDefinition/humanname-own-prefix", "valueString": "VV" } ] }, "given": [ "Bénédicte" ] }, "telecom": [ { "system": "phone", "value": "+33 (237) 998327" } ], "address": { "type": "both", "line": [ "534 Erewhon St" ], "city": "PleasantVille", "postalCode": "3999", "period": { "start": "1974-12-25" } }, "gender": "female", "period": { "start": "2012" } } ],

"managingOrganization": { "reference": "Organization/1" }

}"""

tokenUrl = "https://fhir.epic.com/interconnect-fhir-oauth/oauth2/token"

epicNonProductionClientId = '3f107412-c54f-45d3-a0dd-50fca8c29d67'

signingKey = 'epic_fhir_sandbox_privatekey.pem'

oauthInfo = {'token_url':tokenUrl, "iss":epicNonProductionClientId, 'signing_key':signingKey}

resourceUrl = 'https://fhir.epic.com/interconnect-fhir-oauth/api/FHIR/R4/Patient'

response = engine.fhirPost(resourceUrl, timeout=30, oauth=oauthInfo, body=requestBody)

print response

 

The above code doesn't supply these values, because the defaults are acceptable:

 

OAuth type: client credentials / client secret

 

This code sample below is adapted from working code which searches for patients named "smith" on the Cerner FHIR sandbox. If you want to run this code, you will need to create your own Cerner account.

 

clientId = 'ecda24e6-9d4f-44b5-b6ad-170c1f7bacbc'

clientSecret = 'AnjIe02sGGJKFRpIvbjARvMOHNo6q'

tokenUrl = 'https://authorization.cerner.com/tenants/ec2458f2-1e24-41c8-b71b-0e701af7583d/protocols/oauth2/profiles/smart-v1/token'

scope = 'system/Observation.read system/Patient.read'

oauthInfo = {'scope': scope, 'token_url': tokenUrl, 'client_id': clientId, 'client_secret': clientSecret}

resourceUrl = 'https://fhir-myrecord.cerner.com/r4/ec2458f2-1e24-41c8-b71b-0e701af7583d/Patient?family=smith'

response = engine.fhirGet(resourceUrl, oauth=oauthInfo)

print response