refresh_token在React App上从Auth0获取null



我试图在登录后从Auth0获得刷新令牌。我目前正在使用一个React应用程序与Auth0Lock登录。根据各种来源的建议,我启用了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

  • 确保在您的第一个/authorize请求中添加范围offline_access

反应auth0:

  • 在用户登录时获取刷新令牌,将useRefreshTokens={true}作为prop传递给<Auth0Provider.../>。(默认设置为false)

我也有类似的问题。我遵循了上面提到的所有步骤。这是我的申请细节。

我有静态React web应用程序。我正在部署的应用程序到节点快车服务器。使用localhost访问应用程序。我使用auth0-react 2.0.0 react组件和示例代码已从以下githubhttps://github.com/auth0-developer-hub/spa_react_javascript_hello-world/tree/basic-authentication

我要求在过期时使用refreshToken获取accessToken。但是,https:///oauth/token URL将返回除refreshToken以外的所有内容。所以当原来的accessToken过期时,我无法获得新的accessToken。

启用离线访问,并为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

注意:我已经尝试了内存/本地存储作为cacheLocation。在这两种情况下,在令牌调用时都不会获取refreshToken。

getaccesstokensilence方法没有参数。

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应用程序设置下启用"刷新令牌旋转"one_answers"刷新令牌旋转"属性来解决问题。

输入图片描述

最新更新