我正在 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 作为请求数据的端点来自。
对于上面的网址,在我的获取请求中,我传递以下标头:
'接受':'application/fhir+json',
'内容类型':'application/fhir+json',<- I've also tried removing this as it isn't explicitly stated to be passed
'首选':'响应异步',
'授权':'承载'+令牌
我使用会话来处理请求,并通过覆盖 requests.Session 中的 NoRebuildAuthSession 来保留授权标头
get 请求的响应返回 401 错误,并在 www-authenticate 下显示以下信息: 承载错误 =“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)
正如 Cooper 提到的,您最好的资源是直接联系 Epic,以获取其沙箱/API 的故障排除帮助。
也就是说,浏览一下您的代码,URL 似乎确实是问题所在。看起来您正在模仿 Epic 的 Bulk FHIR 教程,该教程可能会也可能不会在沙箱中完全重现。
在这里,您将调用 Epic on FHIR 端点来获取 OAuth 令牌:
x = session.post('https://fhir.epic.com/interconnect-fhir-oauth/oauth2/token', headers=headers, data=data)
但是,您可以继续使用 App Orchard 端点进行实际的批量 FHIR 调用:
base = "https://apporchard.epic.com/interconnect-aocurprd-oauth/api/FHIR/R4/Group/eIscQb2HmqkT.aPxBKDR1mIj3721CpVk1suC7rlu3yX83/$export"
这是不正确的。假设您尝试在 FHIR 上使用 Epic,要使用的基本 URL 是
https://fhir.epic.com/interconnect-fhir-oauth
,因此该行应为 base = "https://fhir.epic.com/interconnect-fhir-oauth/api/FHIR/R4/Group/..."
之类的内容。
Ultraub,你有没有让它发挥作用 - 我刚刚开始尝试从沙箱测试批量导出,我希望你能为我省去很多麻烦。
谢谢丹尼斯