【发布时间】:2021-12-08 20:07:07
【问题描述】:
我正在 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 请求中,我传递了以下标头:
'接受':'application/fhir+json',
'Content-type' : 'application/fhir+json',
'Prefer':'respond-async',
“授权”:“持有者”+令牌
-
我使用会话来处理请求,并通过覆盖 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)
【问题讨论】:
-
嗨,欢迎来到 SO!我一开始在 HL7 聊天室发现了你的问题。建议您在此处复制/粘贴您的代码。
-
@Cargo23 添加了代码。谢谢!
-
对于沙盒问题,我们希望人们联系 open@epic.com 寻求故障排除帮助。