React Native Firebase谷歌登录反复出现不可恢复登录失败错误求助
问题描述
近10天反复遇到同一问题:使用React Native CLI开发,尝试通过Firebase实现Google社交登录时,持续出现「Error: A non-recoverable sign in failure occurred」错误。
相关代码与配置
主App.js文件
import { Button, StyleSheet, Text, View } from 'react-native' import React from 'react' import auth from '@react-native-firebase/auth'; import { GoogleSignin } from '@react-native-google-signin/google-signin'; export default function App() { GoogleSignin.configure({ webClientId: '22141653705-prn44vqplfc5gnnvta1agv60mqb59tht.apps.googleusercontent.com', }); const signInWithGoogleAsync = async () => { // Get the users ID token try{ const { idToken } = await GoogleSignin.signIn(); // Create a Google credential with the token const googleCredential = auth.GoogleAuthProvider.credential(idToken); // Sign-in the user with the credential const user_sign_in = auth().signInWithCredential(googleCredential); user_sign_in.then((user) => { console.log(user); }) .catch((error)=>{ console.log(error); }) } catch(error){ console.log(error); } } return ( <View style={{flex:1,justifyContent:'center',alignItems:'center'}}> <Button title="Sign in with Google" onPress={signInWithGoogleAsync} /> </View> ) } const styles = StyleSheet.create({})
google-services.json文件
{ "project_info": { "project_number": "22141653705", "project_id": "rn-firebase-c65e0", "storage_bucket": "rn-firebase-c65e0.appspot.com" }, "client": [ { "client_info": { "mobilesdk_app_id": "1:22141653705:android:412f6758b9a9e4c400b318", "android_client_info": { "package_name": "com.testfirebase" } }, "oauth_client": [ { "client_id": "22141653705-jmmnfqj5g1vsjd10b3aneobfqopcdas0.apps.googleusercontent.com", "client_type": 1, "android_info": { "package_name": "com.testfirebase", "certificate_hash": "203e8b51da549d3be63bb70183478696cda25b5f" } }, { "client_id": "22141653705-prn44vqplfc5gnnvta1agv60mqb59tht.apps.googleusercontent.com", "client_type": 3 } ], "api_key": [ { "current_key": "AIzaSyD2pTmhBUQA4ZtT6WEBuG88wAdnmY8Mw1Q" } ], "services": { "appinvite_service": { "other_platform_oauth_client": [ { "client_id": "22141653705-prn44vqplfc5gnnvta1agv60mqb59tht.apps.googleusercontent.com", "client_type": 3 } ] } } } ], "configuration_version": "1" }
额外配置说明
- 已配置调试Android测试用的SHA-1密钥
- WebClientId与google-services.json中client_type为3的client_id一致
- Firebase控制台已添加对应SHA指纹
解决方案排查步骤
1. 确认SHA-1指纹完全匹配
检查Firebase控制台中添加的SHA-1指纹,是否和本地调试/打包用的签名文件指纹完全一致:
- 调试模式下,执行命令
keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android获取指纹,对比控制台中的值,注意大小写、空格都要完全匹配 - 若为打包测试,需额外添加正式签名文件的SHA-1指纹到Firebase控制台
2. 校验OAuth客户端配置
确保Firebase控制台的「OAuth 2.0客户端ID」中,Android类型客户端(client_type=1)的信息正确:
- 包名必须和
android/app/build.gradle中的applicationId完全一致(当前为com.testfirebase) - 该Android客户端的SHA-1需与当前使用的签名指纹匹配
3. 修复代码异步处理逻辑
代码中auth().signInWithCredential(googleCredential)为异步操作,建议统一用async/await处理,同时添加Google Play服务检测,获取更详细的错误信息:
const signInWithGoogleAsync = async () => { try { await GoogleSignin.hasPlayServices(); // 提前检测Google Play服务可用性 const { idToken } = await GoogleSignin.signIn(); const googleCredential = auth.GoogleAuthProvider.credential(idToken); const user = await auth().signInWithCredential(googleCredential); console.log(user); } catch (error) { console.error('登录错误详情:', error.code, error.message); } }
4. 重新更新配置文件并构建项目
每次修改Firebase控制台配置后,重新下载google-services.json替换到android/app目录,然后清理缓存并重新构建:
cd android && ./gradlew clean && cd .. && npx react-native run-android
5. 检查Google API服务状态
登录Google云控制台,找到对应项目,确认「Google Identity Toolkit API」和「Google Sign-In API」已启用。
6. 验证设备环境
- 确保测试设备/模拟器可正常访问Google服务
- 模拟器需安装Google Play服务,且已登录有效的Google账号
内容的提问来源于stack exchange,提问作者Osama Shamim
相关产品推荐
相关产品推荐

