我正在 Python 中实现 FHIR 集成,以从 Epic 的沙盒环境中提取数据。我想看看是否有其他人尝试过此操作,或者在执行启动请求时是否遇到过令牌身份验证问题。
关于我的构建/进度的一些值得注意的信息:
我正在使用https://fhir.epic.com/interconnect-fhir-oauth/oauth2/token获取令牌
传回的 JWT 有效且已签名
根据 Epic 的批量导出规范,我使用https://apporchard.epic.com/interconnect-aocurprd-oauth/api/FHIR/R4/Group/eIscQb2HmqkT.aPxBKDR1mIj3721CpVk1suC7rlu3yX83/$export作为请求数据的端点。
对于上面的 url,在我的 get 请求中,我传递了以下标头:
'接受':'应用程序/fhir+json',
'Content-type' : 'application/fhir+json', <- 我也试过删除它,因为它没有明确声明要通过
'首选':'响应异步',
“授权”:“持有者”+令牌
我使用会话来处理请求,并通过覆盖 requests.Session 中的 NoRebuildAuthSession 来保留 Authorization 标头
get 请求的响应返回 401 错误,并在 www-authenticate 下显示以下信息:Bearer error="invalid_token", error_description="提供的访问令牌无效"
任何指导都会有所帮助!我想我可能使用了错误的端点,但我现在完全被难住了。
class NoRebuildAuthSession(Session):
def rebuild_auth(self, prepared_request, response):
"""
No code here means requests will always preserve the
Authorization
header when redirected.
"""
session = NoRebuildAuthSession()
logging.basicConfig(level=logging.DEBUG)
instance = jwt.JWT()
message = {
# Client ID for non-production
'iss': '<client-id>',
'sub': '<client-id>',
'aud': 'https://fhir.epic.com/interconnect-fhir-oauth/oauth2/token',
'jti': '<string of characters>',
'iat': get_int_from_datetime(datetime.now(timezone.utc)),
'exp': get_int_from_datetime(datetime.now(timezone.utc) + timedelta(minutes=1)),
}
# Load a RSA key from a PEM file.
with open('<private-key-path>', 'rb') as fh:
signing_key = jwt.jwk_from_pem(fh.read())
compact_jws = instance.encode(message, signing_key, alg='RS384')
headers = CaseInsensitiveDict()
headers['Content-Type'] = 'application/x-www-form-urlencoded'
data = {
'grant_type': 'client_credentials',
'client_assertion_type': 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
'client_assertion': compact_jws
}
x = session.post('https://fhir.epic.com/interconnect-fhir-oauth/oauth2/token', headers=headers, data=data)
responseDict = json.loads(x.text)
token = x.json()['access_token']
print(responseDict['access_token'])
headers = {
'Accept':'application/fhir+json',
'Content-type' : 'application/fhir+json',
'Prefer':'respond-async',
'Authorization' : 'Bearer ' + token
}
session.headers = headers
base = "https://apporchard.epic.com/interconnect-aocurprd-oauth/api/FHIR/R4/Group/eIscQb2HmqkT.aPxBKDR1mIj3721CpVk1suC7rlu3yX83/$export"
y = session.get(base, headers = headers)
print(yaml.dump(y.headers, default_flow_style=False))
print(y.text)