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

Expo独立Android应用Google登录完成后无法重定向回应用求助

问题

在Android模拟器上,使用Expo构建的独立APK进行Google登录时,完成登录后无法重定向回应用,停留在登录页面;但在Expo Go环境下,Android和iOS端的Google登录均正常工作。

登录函数代码

try {
    const result = await promptAsync();
    if (result.type === "success") {
        /* `accessToken` is now valid and can be used to get data from the Google API with HTTP requests */
        const { id_token } = result.params;

        const provider = new firebase.auth.GoogleAuthProvider();

        const credential =
            firebase.auth.GoogleAuthProvider.credential(id_token);
        
        auth.signInWithCredential(credential)
            .then((res) => {
                const user = res.additionalUserInfo.profile;
                let action = addUserOnFirestore(
                    res.user?.uid,
                    user.email,
                    user.given_name,
                    user.family_name,
                    user.picture,
                    res
                );
                
                setIsLoading(true);
                try {
                    dispatch(action);
                } catch (err) {
                    setError(err.message);
                }
                setIsLoading(false);
            })
            .catch((error) => {
                console.log("firebase cred err:", error);
            });
    } else {
        console.log("cancelled");
    }
} catch (e) {
    console.log("general error : ", e);
    return { error: true };
}

配置代码

const [request, response, promptAsync] = Google.useIdTokenAuthRequest({
    clientId: "XXXX",
    iosClientId: "XXX",
    androidClientId: "XXX",
    androidStandaloneAppClientId: "XXX",
    redirectUri: Platform.select({
        // iOS handles redirectUri perfectly fine on it's own
        ios: undefined,
        // Due to Expo's bug, we need to manually encode the redirectUri
        // https://github.com/expo/expo/issues/12044
        android: makeRedirectUri({
            // intent filter set up in app.config.js
            // must be the same as "package name" in Google Cloud Console
            native: 'packagename://oauthredirect',
        }),
    })
});

可能的问题根源及解决方法

  • RedirectUri与Intent Filter不匹配:检查app.config.js中的intent filter配置,确保scheme和host与redirectUri里的packagename://oauthredirect完全一致。独立APK依赖intent filter捕获重定向请求,这是和Expo Go的核心差异点。
  • Android独立客户端ID配置错误:androidStandaloneAppClientId必须是Google Cloud Console中为独立APK创建的OAuth客户端ID,包名要与app.config.js中的android.package一致,签名证书指纹也要匹配,不能和Expo Go的客户端ID混用。
  • RedirectUri编码异常:打印redirectUri的最终生成值,确认是packagename://oauthredirect,没有额外的编码错误。
  • 模拟器Google Play服务版本过低:旧版本模拟器的Google Play服务可能不兼容最新OAuth重定向逻辑,尝试更新模拟器的Google Play服务,或更换更高版本的Android系统镜像。
  • Firebase配置遗漏:检查Firebase控制台是否添加了独立APK的SHA-1指纹,且OAuth客户端ID已正确关联到Firebase项目。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 00:35:21