我正在尝试在 go 编程语言上实现 EPIC FHIR SMART 后端服务(后端 OAuth 2.0) 。
我已经创建了我的开发帐户,在那里上传了公钥,并选择backend system作为应用程序受众。
我很确定我的 jwt 令牌是正确的。我在 jwt.io 上检查过,签名是正确的。但是,我总是收到此错误:
{“错误”:“invalid_client”,“error_description”:null }
我也尝试过其他可能的解决方案,例如:
application/x-www-form-urlencodedclient_idRS384)我应该怎么做才能解决这个问题?
顺便说一句,我还在谷歌小组上看到了一些讨论,说在创建开发帐户后等待一两天是值得的。
下面是我的代码。感谢您的帮助!
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)
Run Code Online (Sandbox Code Playgroud)
nov*_*ung 10
太棒了,我现在可以使用了。
解决办法就是等待!这很令人困惑,因为我在文档中找不到任何关于此的解释,而且错误消息也不太友好。
总之,在创建开发应用程序并将公钥上传到那里后,我们必须等待几个小时/几天,然后凭证最终才能使用。
等待部分适用于开放的史诗和应用程序果园开发帐户。
以防万一有人在几个小时后仍然遇到相同的错误,可能值得检查 EPIC 的以下指南。
https://fhir.epic.com/Resources/jwt_auth_troubleshoot_eof