首页 > 解决方案 > [OAuth][Keycloak] 尝试使用 refresh_token 时,invalid_grant 会话不活动

问题描述

我正在尝试使用 ClientOAuth2.Token.refresh() 刷新 oauth2 令牌,但有时会不断收到错误消息: {"error":"invalid_grant","error_description":"Session not active"}

这是我由 Fiddler 捕获的请求

POST [URL]= HTTP/1.1
Host: [URL]
Connection: keep-alive
Content-Length: 2250
Accept: application/json, application/x-www-form-urlencoded
Origin: http://localhost:8080
Authorization: Basic YXNpbW92LWRldi1laGlzLXdlYjo=
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36
Sec-Fetch-Mode: cors
Content-Type: application/x-www-form-urlencoded
Sec-Fetch-Site: cross-site
Referer: http://localhost:8080/
Accept-Encoding: gzip, deflate, br
Accept-Language: en,vi;q=0.9,de;q=0.8,vi-VN;q=0.7,en-US;q=0.6,en-AU;q=0.5

refresh_token=[token]&grant_type=refresh_token

我在 KeyCloak 帐户上的设置可能有问题。有什么建议吗?

标签: oauth-2.0keycloakopenid-connectrefresh-token

解决方案


您需要在检索原始令牌时添加范围offline_access,否则当相关用户会话不活动时您将无法刷新它(这是特定于OIDC,而不是oauth2)。


推荐阅读