首页 > 解决方案 > Keycloak 可以使用 cUrl 获取令牌,但不能使用 javascript httprequest

问题描述

在 cUrl 的帮助下,我能够从 Keycloak 获得令牌。但不是在 HTML 文件中使用带有 Javascript 的 httprequest。这里是请求数据:

     fetch ('http://localhost:8080/auth/realms/MyRealm/protocol/openid-connect/token', {
             method: 'post',
             body: "client_id=xxx&password=yyy&username=testuser&grant_type=password&client_secret=633ebfee-5200-4cef-a12f-9196a7597334&scope=openid",
             headers: {
                 'Content-Type': 'application/x-www-form-urlencoded
             },
             mode: 'no-cors'
         })

答案不是令牌,而是以下对象:

    body: null
    bodyUsed: false
    headers: Headers
    ok: false
    redirected: false
    status: 0
    statusText: ""
    type: "opaque"
    url: ""

HTML 文件作为 localhost 在本地 Web 服务器上运行。当我通过 Httprequest 调用它时,我需要改变什么?

标签: curlhttprequesttokenkeycloak

解决方案


推荐阅读