You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用expo-auth-session/providers/google无法获取Refresh Token的问题

Expo中Google OAuth无法获取Refresh Token的问题

我在使用Expo开发React Native应用时,调用useAuthRequest无法从返回结果中获取refresh token。点击登录按钮执行promptAsync({useProxy:true, showInRecents: true})后,useEffect触发并输出返回结果,其中refreshToken为undefined。我已在Google.useAuthRequest中配置accessType: 'offline',并尝试了多种参数组合,但问题仍未解决。相关代码如下:

const [auth, setAuth] = useState(null);

const [request, response, promptAsync] = Google.useAuthRequest(
        {
            iosClientId: keys.iosClientId,
            androidClientId: keys.androidClientId,
            expoClientId: keys.expoClientId,
            accessType: 'offline'
        },
    );

    useEffect(() => {
        console.log('useeffect 1');
        if (response?.type === 'success') {
            setAuth(response.authentication);
            console.log(response.authentication.refresh_token); //WHY IS IT UNDEFINED????
            const persistAuth = async () => {
                await AsyncStorage.setItem(
                    'auth',
                    JSON.stringify(response.authentication)
                );
            };

            persistAuth();
            getUserData(response.authentication);
        }
    }, [response]);

可能的解决方向

  • 检查Google Cloud Console客户端配置
    确保iOS、Android、Expo对应的OAuth客户端ID都开启了离线访问权限,同时确认OAuth同意屏幕已发布(非测试状态),未发布的同意屏幕会限制权限返回。

  • 添加prompt: 'consent'参数
    Google OAuth默认在用户已授权后不会重复返回refresh token,添加该参数可强制用户重新授权,从而获取refresh token。修改后的配置:

    {
        iosClientId: keys.iosClientId,
        androidClientId: keys.androidClientId,
        expoClientId: keys.expoClientId,
        accessType: 'offline',
        prompt: 'consent'
    }
    
  • 修正refresh token字段名
    Expo Google Auth返回的refresh token字段是驼峰命名的refreshToken,而非refresh_token。代码中应改为console.log(response.authentication.refreshToken)。

  • 清除用户端授权记录
    若用户之前已授权过应用,Google会缓存授权状态,导致无法获取新的refresh token。让用户前往Google账户的安全板块,找到第三方应用访问权限,删除你的应用授权后重新登录测试。

内容的提问来源于stack exchange,提问作者Perplexed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 04:25:17