【发布时间】:2021-09-09 16:07:20
【问题描述】:
我正在尝试实现 EPIC FHIR SMART Backend Services (Backend OAuth 2.0) on go 编程语言。
我已经创建了我的开发帐户,在那里上传了公钥,并选择了 backend system 作为应用程序受众。
我很确定我的 jwt 令牌是正确的。我在 jwt.io 上检查过,签名是正确的。但是,我总是收到此错误:
{ "error": "invalid_client", "error_description": null }
我也尝试了其他可能的解决方案,例如:
- 确保喷气式飞机索赔中的到期日期低于 5 分钟
- 将有效负载放置在具有正确内容类型的正文中,即
application/x-www-form-urlencoded - 确保使用沙盒
client_id - 使用正确的 jwt 登录方法 (
RS384)
我应该怎么做才能解决这个问题?
顺便说一句,我还看到several discussions on the 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)
【问题讨论】: