0

我正在 Python 中实现 FHIR 集成,以从 Epic 的沙盒环境中提取数据。我想看看是否有其他人尝试过此操作,或者在执行启动请求时是否遇到过令牌身份验证问题。

关于我的构建/进度的一些值得注意的信息:

  1. 我正在使用https://fhir.epic.com/interconnect-fhir-oauth/oauth2/token获取令牌

  2. 传回的 JWT 有效且已签名

  3. 根据 Epic 的批量导出规范,我使用https://apporchard.epic.com/interconnect-aocurprd-oauth/api/FHIR/R4/Group/eIscQb2HmqkT.aPxBKDR1mIj3721CpVk1suC7rlu3yX83/$export作为请求数据的端点。

  4. 对于上面的 url,在我的 get 请求中,我传递了以下标头:

    '接受':'应用程序/fhir+json',

    'Content-type' : 'application/fhir+json', <- 我也试过删除它,因为它没有明确声明要通过

    '首选':'响应异步',

    “授权”:“持有者”+令牌

  5. 我使用会话来处理请求,并通过覆盖 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)
4

1 回答 1

0

正如 Cooper 所提到的,您最好的资源是直接联系 Epic 以获取有关其沙盒/API 的故障排除帮助。

也就是说,浏览您的代码,URL 似乎确实是问题所在。您似乎在模仿 Epic 的 Bulk FHIR 教程,它在沙盒中可能完全可重现,也可能不完全重现。

在这里,您在 FHIR 端点上调用 Epic 以获取 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/...".

于 2021-12-11T01:29:05.993 回答