我试图在登录后从 Auth0 获取刷新令牌。我目前正在使用带有 Auth0Lock 的 React 应用程序登录。根据各种来源的建议,我启用了
Allow Offline Access
并将范围设置为offline_access
。
但我仍然认为
refresh_token
为空。任何帮助或建议的方法来实现refresh_token
?
选中此点以从 Auth0 检索
refresh_token
:
为您的 API 启用
Allow Offline Access
,转到Applications > APIs > [YourApi] > Access Settings
.
为您的应用程序启用
Grant Types
,转到Applications > [YourSpaApp] > Advanced Settings > Grant Types
并检查refresh_token
.
确保在您的第一个
offline_access
请求中添加范围/authorize
。
反应 auth0:
useRefreshTokens={true}
作为道具传递给<Auth0Provider.../>
。 (默认设置为false
)我也有类似的问题。我已按照上述所有步骤进行操作。这是我的申请详情。
我有 Static React Web 应用程序。我正在将该应用程序部署到 Node Express 服务器中。使用本地主机访问应用程序。 我正在使用 auth0-react 2.0.0 反应组件,示例代码取自以下 github
https://github.com/auth0-developer-hub/spa_react_javascript_hello-world/tree/basic-authentication
.
我要求accessToken过期时使用refreshToken获取。但是,https://
启用离线访问并为 Auth0 应用程序选择刷新令牌授予。当我们尝试从其他编程语言获取刷新令牌时,它起作用了。
Auth0 Provider code:
const domain = “valid_auth0_domain”;
const clientId = “valid_client_id”;
const redirectUri = “http://localhost:8080/callback”;
const connectionId = “valid connectionid”;
const scope = “openid offline_access”;
const audience = “valid audience”;
const promptType = “login”;
return (
<Auth0Provider
domain={domain}
clientId={clientId}
authorizationParams={{
useRefreshTokens: true,
cacheLocation: ‘localstorage’,
scope: ‘openid offline_access’,
redirect_uri: redirectUri,
audience: audience,
connection: connectionId
}}
onRedirectCallback={onRedirectCallback}
>
{children}
);
授权调用请求:
https://.auth0.com/authorize?client_id=<client_id>&scope=openid+offline_access&domain=.auth0.com&useRefreshTokens=true&cacheLocation=localstorage&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Fcallback&audience=https%3A%2F%2FXXX.co&connection=XYZ&prompt=login&response_type=code&response_mode=query&state=ZHBxYVBZMy1ja2hQRmVEekl1a1BJZ040S1lhR3lGV0t%2BbFNoeDBaZllmNw%3D%3D&nonce=SzNoSUtadVVDfmVCLXZydWlBZnREMFIwQTIzT3pDVkN3ZkpET3BFLmg5dw%3D%3D&code_challenge=G_gEA3lWJ46wzdFHTH2ppRUmYO0B5HCS-t54s7Y_oWU&code_challenge_method=S256&auth0Client=eyJuYWXYZXX
注意:我已经尝试将 memory/localstorage 作为 cacheLocation。在这两种情况下,都不会在令牌调用时获取 refreshToken。
getAccessTokenSilently 方法没有参数。
const token = await getAccessTokenSilently();
请求负载:
client_id=<client_id>&code_verifier=_GAb6EDotFpJyzB%7E%7EIhDmB4Z1HjuAN-3ydF1Zqj2_bK&grant_type=authorization_code&code=XXXXX&redirect_uri=http://localhost:8080/callback
授权令牌调用响应:
access_token: “<acces_token>”
expires_in: 1800
id_token: “<id_token>”
scope: “openid offline_access”
token_type: “Bearer”
非常感谢您的回复。
谢谢 拉克西米
----更新----- 我通过在 Auth0 页面的 Auth0 应用程序设置下启用“刷新令牌轮换”和“刷新令牌轮换”属性解决了这个问题。