Expo React Native应用Google登录:开发构建正常预览构建报错400
Expo React Native iOS预览构建Google登录出现redirect_uri_mismatch错误
问题现象
使用Expo + React Native开发应用,集成Firebase实现Google登录功能。iOS开发构建(developmentClient: true)中登录功能运行正常,但iOS预览构建(developmentClient: false)时出现以下错误:
Access blocked: This app's request is invalid You can't sign in because this app sent an invalid request. You can try again later, or contact the developer about this issue. Error 400: redirect_uri_mismatch
点击错误详情仅显示Error 400: redirect_uri_mismatch。
已确认事项
- Apple标识符、Google Console iOS客户端ID、Firebase应用的Bundle ID均统一为
com.username.appname - 连接测试设备查看控制台日志,未发现有效排查线索
疑问
是否有开发者遇到过相同问题?或者当developmentClient设为false时,相关环境变量或配置的变化是否能提供排查方向?
相关配置代码
依赖配置(package.json)
"dependencies": { "@react-native-async-storage/async-storage": "~1.17.11", "@react-navigation/native": "^6.1.2", "@react-navigation/native-stack": "^6.9.8", "expo": "^48.0.11", "expo-auth-session": "~4.0.3", "expo-dev-client": "~2.2.1", "expo-image-picker": "~14.1.1", "expo-linear-gradient": "~12.1.2", "expo-random": "~13.1.1", "expo-splash-screen": "~0.18.1", "expo-status-bar": "~1.4.4", "expo-web-browser": "^12.1.1", "firebase": "^9.16.0", "nativewind": "^2.0.11", "react": "18.2.0", "react-native": "0.71.6", "react-native-dotenv": "^3.4.7", "react-native-safe-area-context": "^4.5.1", "react-native-safearea-height": "^1.0.5", "react-native-screens": "^3.20.0" }, "devDependencies": { "@babel/core": "^7.20.12", "tailwindcss": "^3.2.4" },
登录页面核心代码(LoginScreen.js)
//LoginScreen.js import React, { useEffect } from 'react'; import * as Google from 'expo-auth-session/providers/google'; import { IOS_CLIENT_ID, EXPO_CLIENT_ID, ANDROID_CLIENT_ID } from '@env'; import { makeRedirectUri } from 'expo-auth-session'; import { GoogleAuthProvider, signInWithCredential, } from '@firebase/auth'; import { auth } from '../firebase'; const LoginScreen = () => { const [request, response, promptAsync] = Google.useAuthRequest({ iosClientId: IOS_CLIENT_ID, expoClientId: EXPO_CLIENT_ID, adroidClientID: ANDROID_CLIENT_ID, redirectUri: makeRedirectUri({ scheme: 'com.username.appname' }), }); useEffect(() => { if (response?.type == 'success') { const idToken = response['authentication']['idToken']; const accessToken = response['authentication']['accessToken']; const credential = GoogleAuthProvider.credential(idToken, accessToken); signInWithCredential(auth, credential); } }, [response]); const signInWithGoogle = () => { promptAsync(); }; }
EAS构建配置(eas.json)
//eas.json { "cli": { "version": ">= 2.7.1" }, "build": { "development": { "developmentClient": true, "distribution": "internal" }, "preview": { "distribution": "internal" }, "production": {} }, "submit": { "production": {} } }
Expo应用配置(app.json)
//app.json { "expo": { "name": "appname", "slug": "appname", "scheme": "com.username.appname", "version": "1.0.0", "orientation": "portrait", "icon": "./assets/icon.png", "userInterfaceStyle": "light", "plugins": [ [ "expo-image-picker", { "photosPermission": "Allow access to photos to add photos.", "cameraPermission": "Allow access to camera to take photos." } ] ], "splash": { "image": "./assets/splashscreen.png", "resizeMode": "contain", "backgroundColor": "#000000" }, "updates": { "fallbackToCacheTimeout": 0 }, "assetBundlePatterns": [ "**/*" ], "ios": { "supportsTablet": true, "bundleIdentifier": "com.username.appname" }, "android": { "adaptiveIcon": { "foregroundImage": "./assets/adaptive-icon.png", "backgroundColor": "#FFFFFF" }, "package": "com.username.appname" }, "web": { "favicon": "./assets/favicon.png" }, "extra": { "eas": { "projectId": "1aaaa111-aa11-1aaa-aaaa-1a11aa1a11a1" } } } }
Firebase初始化配置(firebase.js)
//firebase.js import { initializeApp } from 'firebase/app'; import { getAnalytics } from 'firebase/analytics'; import { getFirestore } from 'firebase/firestore'; import { getStorage } from 'firebase/storage'; import { firebaseConfig_apiKey, firebaseConfig_authDomain, firebaseConfig_projectId, firebaseConfig_storageBucket, firebaseConfig_messagingSenderId, firebaseConfig_appId, firebaseConfig_measurementId, } from '@env'; import { initializeAuth, getReactNativePersistence, } from 'firebase/auth/react-native'; import AsyncStorage from '@react-native-async-storage/async-storage'; const firebaseConfig = { apiKey: firebaseConfig_apiKey, authDomain: firebaseConfig_authDomain, projectId: firebaseConfig_projectId, storageBucket: firebaseConfig_storageBucket, messagingSenderId: firebaseConfig_messagingSenderId, appId: firebaseConfig_appId, measurementId: firebaseConfig_measurementId, }; const app = initializeApp(firebaseConfig); const auth = initializeAuth(app, { persistence: getReactNativePersistence(AsyncStorage), }); const storage = getStorage(app); const db = getFirestore(); export { auth, db, storage };
相关截图
- 错误消息截图:

- 错误详情截图:

- Google控制台配置截图1:

- Google控制台配置截图2:

内容的提问来源于stack exchange,提问作者Alex
相关产品推荐
相关产品推荐

