我正在尝试实施 EPIC FHIR SMART 后端服务(后端 OAuth 2.0) 编程语言。
我已经创建了我的开发帐户,在那里上传了公钥,并选择了
backend system
作为应用程序受众。
我很确定我的 jwt 令牌是正确的。我已经在 jwt.io 上检查过了,签名是正确的。但是,我总是得到这个错误:
{“错误”:“invalid_client”,“error_description”:空}
我也尝试过其他可能的解决方案,例如:
application/x-www-form-urlencoded
client_id
RS384
)我应该怎么做才能解决这个问题?
顺便说一句,我也看到了google groups上的几个讨论说在创建开发帐户后等待一两天是值得的
下面是我的代码。感谢帮助!
var (
oauth2TokenUrl = "https://fhir.epic.com/interconnect-fhir-oauth/oauth2/token"
sandboxClientID = "..."
privateKey = "..."
)
// load private key
signKey, err := jwt.ParseRSAPrivateKeyFromPEM([]byte(privateKey))
So(err, ShouldBeNil)
// construct jwt claims
now := time.Now()
claims := jwt.MapClaims{
"iss": sandboxClientID,
"sub": sandboxClientID,
"aud": oauth2TokenUrl,
"jti": uuid.New().String(), // fill with reference id
"exp": now.Add(1 * time.Minute).Unix(), // cannot be more than 5 minutes!
}
log.Info(" => claims:", utility.ToJsonString(claims))
// generate signed token using private key with RS384 algorithm
alg := jwt.SigningMethodRS384
signedToken, err := jwt.NewWithClaims(alg, claims).SignedString(signKey)
So(err, ShouldBeNil)
log.Info(" => signed token", signedToken)
// prepare api call payload
payload := map[string]string{
"grant_type": "client_credentials",
"client_assertion_type": "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",
"client_assertion": signedToken,
}
// dispatch the api call
req := resty.New().
R().
EnableTrace().
SetFormData(payload)
res, err := req.Post(oauth2TokenUrl)
So(err, ShouldBeNil)
log.Info(" => response status:", res.StatusCode())
log.Info(" => response header:", res.Header())
log.Info(" => response body:", string(res.Body()))
// parse response
resBody := make(map[string]interface{})
err = json.Unmarshal(res.Body(), &resBody)
So(err, ShouldBeNil)
太棒了,我现在开始工作了。
解决方法就是等待!这很令人困惑,因为我在文档上找不到关于此的任何解释,而且错误消息也不是很友好。
总而言之,在创建开发应用程序并将公钥上传到那里之后,我们必须等待几个小时/几天,然后凭据最终才能使用。
等待部分适用于open epic和app orchard开发账户。
Epic 似乎有某种同步机制,每天运行一次。因此,在帐户创建后等待是唯一的解决方案。另请注意,在
Endpoint URI
更改后的应用程序设置中,您还需要等待一段时间。
Error
{ "error": "invalid_client", "error_description": null }
当 redirect_uri
参数设置为类似 localhost:3000
. 时也会出现
我也遇到了这个问题。就我而言,我使用“患者”作为为 FHIR 应用程序上的 Epic SMART 选择的“应用程序受众”。我能够在测试服务器上成功获取授权码,但是当我尝试将其交换为访问令牌时,我收到了“invalid_client”错误消息。
我犯的错误是 HTTP POST 中的
redirect_uri
必须是绝对 URL,并且必须与您为应用程序指定的重定向 URI 匹配。如果重定向 URI 无效,则生成的错误消息将显示“无效客户端”(这是误导性的)。
这是我使用的 Python 代码示例...
data = {
'grant_type': 'authorization_code',
'code': request.GET.get('code'),
'redirect_uri': 'http://127.0.0.1:8000/ehr_connection_complete/', # THIS MUST BE AN ABSOLUTE URL
'client_id': '11111111-2222-3333-4444-555555555555',
}
response = post(url, data)
令我感到奇怪的是,
redirect_uri
参数的错误会生成有关 invalid_client
的错误消息,但 Epic 的测试 FHIR 服务器确实如此。
我希望这些信息对其他人有帮助。