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

Expo生产构建中Google认证失效,开发构建正常

问题:Expo发布后Google认证功能失效,开发环境正常

发布到Expo的应用中Google认证功能无法正常工作:下载应用后仅显示Google授权弹窗,完成跳转后无任何反应,既不更新Auth token,也不获取用户数据,但开发构建中功能完全正常。

相关代码

const [accessToken, setAccessToken] = useState();
const [userInfo, setUserInfo] = useState();
const [auth, setAuth] = useState();
const [request, response, promptAsync] = Google.useAuthRequest({
  androidClientId: "xyc",
  iosClientId: "xyc",
  expoClientId: "xyc",
});

useEffect(() => {
  if (response?.type === "success") {
    setAccessToken(response.authentication);

    const persistAuth = async () => {
      await AsyncStorage.setItem(
        "auth",
        JSON.stringify(response.authentication)
      );
    };
    persistAuth();
  }
}, [response]);

useEffect(() => {
  const getPersistedAuth = async () => {
    const jsonValue = await AsyncStorage.getItem("auth");
    if (jsonValue != null) {
      const authFromJson = JSON.parse(jsonValue);
      setAuth(authFromJson);

      let userInfoResponse = await fetch(
        "https://www.googleapis.com/userinfo/v2/me",
        {
          headers: { Authorization: `Bearer ${authFromJson.accessToken}` },
        }
      );
      userInfoResponse.json().then((data) => {
        setUserInfo(data);
      });

      setRequireRefresh(
        !AuthSession.TokenResponse.isTokenFresh({
          expiresIn: authFromJson.expiresIn,
          issuedAt: authFromJson.issuedAt,
        })
      );
    }
  };
  getPersistedAuth();
}, [response]);

app.json配置

{
  "expo": {
    "name": "deliveroo",
    "slug": "deliveroo",
    "version": "1.2.0",
    "orientation": "portrait",
    "icon": "./assets/icon.png",
    "userInterfaceStyle": "light",
    "scheme": "com.hammadsohaill786.project",
    "splash": {
      "image": "./assets/splash.png",
      "resizeMode": "contain",
      "backgroundColor": "#ffffff"
    },
    "updates": {
      "fallbackToCacheTimeout": 0,
      "url": "xyc"
    },
    "assetBundlePatterns": [
      "**/*"
    ],
    "ios": {
      "supportsTablet": true,
      "bundleIdentifier": "com.hammadsohaill786.project"
    },
    "android": {
      "googleServicesFile": "./google-services.json",
      "adaptiveIcon": {
        "foregroundImage": "./assets/adaptive-icon.png",
        "backgroundColor": "#FFFFFF"
      },
      "package": "com.hammadsohaill786.deliveroo"
    },
    "web": {
      "favicon": "./assets/favicon.png"
    },
    "extra": {
      "eas": {
        "projectId": "xyc"
      }
    },
    "plugins": [
      "@react-native-google-signin/google-signin"
    ],
    "runtimeVersion": {
      "policy": "sdkVersion"
    }
  }
}

解决方案

  • 匹配Android包名与OAuth客户端ID:app.json中Android的package为com.hammadsohaill786.deliveroo,但scheme和iOS的bundleIdentifier是com.hammadsohaill786.project,Google Cloud中Android OAuth客户端ID的包名必须与package完全一致,同时签名证书指纹要使用EAS构建生成的发布版指纹,不能用调试版指纹。
  • 统一Scheme配置:确保scheme与Android的package、iOS的bundleIdentifier保持一致,或调整为Google Cloud中配置的回调URL对应的scheme。当前scheme与Android包名不一致,会导致跳转回调失败,无法触发response的success事件。
  • 移除冲突插件:同时使用expo-auth-session的Google.useAuthRequest和@react-native-google-signin/google-signin插件会引发逻辑冲突,建议只保留一套认证方案,比如继续使用expo-auth-session的话,就删除app.json中的@react-native-google-signin/google-signin插件。
  • 修正useEffect依赖:第二个useEffect依赖response不合理,它的作用是读取本地存储的认证信息,应该只在组件挂载时执行一次,将依赖改为空数组,同时把userInfoResponse.json()的链式调用改为await写法避免异步问题:
    useEffect(() => {
      const getPersistedAuth = async () => {
        const jsonValue = await AsyncStorage.getItem("auth");
        if (jsonValue != null) {
          const authFromJson = JSON.parse(jsonValue);
          setAuth(authFromJson);
    
          let userInfoResponse = await fetch(
            "https://www.googleapis.com/userinfo/v2/me",
            {
              headers: { Authorization: `Bearer ${authFromJson.accessToken}` },
            }
          );
          const data = await userInfoResponse.json();
          setUserInfo(data);
    
          setRequireRefresh(
            !AuthSession.TokenResponse.isTokenFresh({
              expiresIn: authFromJson.expiresIn,
              issuedAt: authFromJson.issuedAt,
            })
          );
        }
      };
      getPersistedAuth();
    }, []);
    
  • 验证Google Cloud OAuth配置:在Google Cloud控制台中,确认Android和iOS的OAuth客户端ID已正确配置,对应平台的包名/bundleIdentifier、签名指纹无误,同时添加了Expo对应的回调URL(若使用expoClientId)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 23:25:19