我正在编写一个API,用于在Keyclope服务器中创建一个新客户端.我使用Go斗篷包与Keyclope服务器进行交互.起初,我从Go斗篷中传入了访问令牌.登录()func到Go斗篷.CreateClient()并在使用Go斗篷的访问令牌后出现403错误.LoginAdmin()成功了,它确实创建了一个新客户端.那么,是什么使得从Go斗篷返回的访问令牌.登录()失败?
代码:
func main() {
newClientID := "new_client"
client := gocloak.NewClient("http://localhost:8080")
// The access token returned from Login() causes 403 error
jwt, _ := client.Login(context.Background(), "my-go-service", "vizhhp0qnDGaiq4k0aOzzn4RaaqSwU2b", "master", "admin", "Pa55w0rd")
_, err := client.CreateClient(context.Background(), jwt.AccessToken, "demorealm", gocloak.Client{ ClientID: &newClientID})
if err != nil {
fmt.Println(err.Error())
}
// And the access token returned from LoginAdmin() works
jwt, _ = client.LoginAdmin(context.Background(), "admin", "Pa55w0rd", "master")
clientID, err := client.CreateClient(context.Background(), jwt.AccessToken, "demorealm", gocloak.Client{ ClientID: &newClientID})
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Printf("Client %s created", clientID)
}
}
结果:
403 Forbidden: unknown_error
Client d869fd8d-e5f0-4567-99de-69ccc4403705 created