我正在尝试对公共 FHIR 服务器 (SMART Health IT) 进行 API 调用。我在服务器上使用
ngrok
托管了我的应用程序(前端),我想在从 FHIR 检索所需数据之前获取其授权代码,然后获取访问令牌。我正在使用 SMART Launcher (https://launch.smarthealthit.org/?tab=1&launch=WzAsIiIsIiIsIkFVVE8iLDAsMCwwLCIiLCIiLCIiLCIiLCIiLCIiLCIiLDEsMV0&validation=1) 启动我的应用程序,并在此处传递一些客户端 ID 和客户端密钥。然后,我尝试在代码中使用这些值来检索授权代码和令牌。
我正在使用Python的OAuth2.0库,其中我使用WebApplicationClient来获取授权URL。但是,当我手动转到生成的授权 URL 时,它会重定向到类似的内容
https://URL/?error=invalid_request&error_description=Invalid+launch+options%3A+SyntaxError%3A+Unexpected+end+of+JSON+input
为什么会发生这种情况?我在这里做错了什么吗?下面是我写的代码。另外,我不知道如何在我的代码中以编程方式获取
authorization_response
(假设第一个问题已解决)。
注意:
redirect_uri
是我在ngrok上托管的应用程序的URL。这是一个纯粹的前端应用程序。
client = WebApplicationClient(client_id)
authorize_endpoint = "https://launch.smarthealthit.org/v/r4/auth/authorize"
redirect_uri = "https://URL"
authorization_url = client.prepare_request_uri(
authorize_endpoint,
redirect_uri = redirect_uri
)
print('Authorization URL: ', authorization_url)
parsed_response = client.parse_request_uri_response(authorization_response)
authorization_code = parsed_response["code"]
以上是导致错误的代码部分。我被困在这一步了。我提供了我编写的剩余代码(如下)以供参考和上下文。它假设已成功获取授权码并尝试获取访问令牌。
data = client.prepare_request_body(
code = authorization_code,
redirect_uri = redirect_uri,
client_id = client_id,
client_secret = client_secret
)
print('Token data: ', data)
token_response = requests.post(token_endpoint, data=data, headers={"Content-Type": "application/x-www-form-urlencoded"})
if token_response.status_code == 200:
access_token = token_response.json()["access_token"]
print("Access Token:", access_token)
else:
print("Error obtaining access token:", token_response.text)
根据评论,听起来您打算将其作为从 EHR 启动的 SMART on FHIR 应用程序。在这种情况下,您构建的授权 URL 似乎不完整。你有
authorization_url = client.prepare_request_uri(authorize_endpoint, redirect_uri = redirect_uri)
。这将导致 https://launch.smarthealthit.org/v/r4/auth/authorize?client_id=<client_id>&response_type=code&redirect_uri=https%3A%2F%2FURL
的授权调用。
此电话丢失...
launch
参数,设置为 EHR 提供的启动代码。scope
参数,定义所需的范围。通常,这设置为 launch openid fhirUser
但只有您知道需要什么(在这种情况下需要“启动”)。aud
参数,定义您计划调用的 FHIR 服务器。这是由 EHR 在 iss
参数中提供的。state
参数,该参数是任意的,但出于安全原因使用。SMART IG的解释可能对您有帮助。上述信息假设您正在使用的服务器支持最新版本的 SMART 实施指南。您的服务器可能不需要某些参数。