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

从expo-auth-google迁移到expo-auth-session遇ValidationError求助

Expo Auth Session 谷歌登录 ValidationError 问题解决

问题背景

从废弃的expo-auth-google切换到新版expo-auth-session实现谷歌登录后,出现如下错误:

ValidationError: "redirect_uri" is not allowed. "client_id" is not allowed. "response_type" is not allowed. "state" is not allowed. "scope" is not allowed

已完成GCP和Firebase相关配置,查阅官方文档及教程未解决,代码如下:

import React, {
    createContext,
    useContext,
    useMemo,
    useEffect,
    useState,
} from "react";
import * as Google from "expo-auth-session/providers/google";
import Constants from "expo-constants";
import {
    GoogleAuthProvider,
    onAuthStateChanged,
    signInWithCredential,
    signOut,
} from "firebase/auth";
import { auth } from "../firebase";

const AuthContext = createContext({});

export const AuthProvider = ({ children }) => {
    const [error, setError] = useState(null);
    const [user, setUser] = useState(null);
    const [loadingInitial, setLoadingInitial] = useState(true);
    const [loading, setLoading] = useState(false);

    const [_request, _loginResult, promptGoogle] = Google.useAuthRequest({
        androidClientId: '**',
        iosClientId: '**',
        expoClientId: '**',
        scopes: ["profile", "email", "openid"],
        responseType: "id_token",
    });

    const logout = () => {
        setLoading(true);
        signOut(auth)
            .catch((error) => setError(error))
            .finally(() => {
                setLoading(false);
            });
    };

    const signInWithGoogle = () => {
        setLoading(true);
        promptGoogle()
            .then((loginResult) => {
                if (loginResult?.type === "success") {
                    const idToken = loginResult.params.id_token;
                    const credential = GoogleAuthProvider.credential(idToken);
                    signInWithCredential(auth, credential).then(() => {
                        console.log("Logged in");
                    });
                } else {
                    throw new Error("Google login failed");
                }
            })
            .catch((error) => setError(error))
            .finally(() => setLoading(false));
    };

    useEffect(() => {
        const unsub = onAuthStateChanged(auth, (firebaseUser) => {
            if (firebaseUser) {
                setUser(firebaseUser);
            } else {
                setUser(null);
            }
            setLoadingInitial(false);
        });

        return unsub;
    }, []);

    const memoedValue = useMemo(
        () => ({
            user,
            loading,
            error,
            signInWithGoogle,
            logout,
        }),
        [user, loading, error]
    );

    return (
        <AuthContext.Provider value={memoedValue}>
            {!loadingInitial && children}
        </AuthContext.Provider>
    );
};

export default function useAuth() {
    return useContext(AuthContext);
}

解决方案

1. 修正GCP OAuth客户端的重定向URI配置

该错误核心是Google OAuth服务器拒绝了请求参数,最常见原因是重定向URI不匹配。需为GCP中对应的Android/iOS/Expo OAuth客户端添加正确的重定向URI:

  • Expo客户端:格式为 https://auth.expo.io/@你的Expo用户名/项目slug,可从app.json的slug字段或Constants.expoConfig.slug获取
  • Android客户端:格式为 com.googleusercontent.apps.你的AndroidClientId:/oauth2redirect
  • iOS客户端:格式为 com.googleusercontent.apps.你的iOSClientId:/oauth2redirect

操作路径:GCP控制台 → 「API和服务」→「凭据」→ 找到对应客户端ID → 点击「编辑」→ 在「授权重定向URI」中添加上述URI。

2. 调整useAuthRequest参数的合法性

确认参数配置符合Expo Auth Session要求:

  • 确保responseType取值正确,使用id_token时必须包含openid scope(你的代码已满足,但需确认无拼写错误)
  • 检查是否存在额外的未声明参数被传入请求,比如是否在其他逻辑中修改了请求参数

3. 同步Firebase与GCP的谷歌登录配置

在Firebase控制台「Authentication」→「登录方法」→「Google」中:

  • 确认已启用谷歌登录
  • 确保添加的Android/iOS客户端ID与GCP中对应客户端ID完全一致
  • 检查「Web客户端ID」是否配置正确(若需Web端支持)

4. 清除Expo项目缓存

执行命令清除缓存并重启项目,避免旧配置残留:

expo r -c

5. 验证SDK版本兼容性

确保expo-auth-session版本与当前Expo SDK版本匹配,建议升级到对应SDK的最新兼容版本,避免版本不兼容导致的参数解析错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 16:27:53