Keycloak 仅在首次登录时获得 Google 刷新令牌

Keycloak only gets Google refresh token on first login

我正在使用带有 Google 的 Keycloak 作为身份提供者。我需要来自 Google 的刷新令牌才能管理用户的日历。这是我的 Keycloak Google IDP 设置:

登录后,我根据https://www.keycloak.org/docs/latest/server_development/index.html#retrieving-external-idp-tokens获取刷新令牌。看起来像这样:

{
    "access_token": "XXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    "expires_in": 3599,
    "refresh_expires_in": 0,
    "refresh_token": "YYYYYYYYYYYYYYYYYYYYYYYYYYYY",
    "token_type": "Bearer",
    "id_token": "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZ",
    "not-before-policy": 0,
    "scope": "openid https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile",
    "accessTokenExpiration": 1593706596
}

现在的问题是,当我第二次登录然后再次尝试获取刷新令牌时,它不见了:

{
    "access_token": "XXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    "expires_in": 3599,
    "refresh_expires_in": 0,
    "token_type": "Bearer",
    "id_token": "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZ",
    "not-before-policy": 0,
    "scope": "https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile openid",
    "accessTokenExpiration": 1593706782
}

我真的不确定这怎么可能。我想到的一件事是 Keycloak 不遵守后续登录的“请求刷新令牌”设置,但我不知道如何验证这一点。

这不是 Keycloak 错误,而是 Google 规范。 refresh_token 仅在用户首次授权时提供。这里是此行为的文档:

refresh_token: A token that you can use to obtain a new access token. Refresh tokens are valid until the user revokes access. Again, this field is only present in this response if you set the access_type parameter to offline in the initial request to Google's authorization server. - source

如果您想再次获取刷新令牌,您必须:

  1. 转到显示有权访问您帐户的应用程序的页面:https://myaccount.google.com/u/0/permissions
  2. 在 Third-party 应用菜单下,选择您的应用。
  3. 单击“删除访问权限”,然后单击“确定”进行确认
  4. 您发出的下一个 OAuth2 请求将 return 一个 refresh_token(前提是它还包括 'access_type=offline' 查询参数。

相关 this answer