React Native无Firebase环境下Google登录失败:不可恢复登录错误
解决Google登录报错“发生了无法恢复的登录失败”
问题场景
我想在用户点击Google登录按钮后获取用户名、邮箱等基础信息,不使用Firebase,但目前遇到错误提示:发生了无法恢复的登录失败。
我的代码如下:
const MainApp = () => { const [user, setUser] = useState({}); useEffect(() => { GoogleSignin.configure({ webClientId: 'XXX', offlineAccess: true, forceCodeForRefreshToken: true, }); isSignedIn(); }, []); const signIn = async () => { try { await GoogleSignin.hasPlayServices(); const userInfo = await GoogleSignin.signIn(); console.log('due-->', userInfo); setUser(userInfo); } catch (error) { console.log('Message-->', error.message); if (error.code === statusCodes.SIGN_IN_CANCELLED) { // user cancelled the sign-in process Alert.alert('user cancelled the sign-in process'); } else if (error.code === statusCodes.IN_PROGRESS) { // operation (e.g. sign in) is in progress already Alert.alert('Sign in is in progress already'); } else if (error.code === statusCodes.PLAY_SERVICES_NOT_AVAILABLE) { // play services not available or outdated Alert.alert('play services not available or outdated'); } else { // some other error occurred Alert.alert(error + ' err'); } } }; const isSignedIn = async () => { const isSignedIn = await GoogleSignin.isSignedIn(); if (!!isSignedIn) { getCurrentUserInfo(); } else { console.log('Please login'); } }; };
排查与解决步骤
核对Web Client ID配置
确保webClientId是从Google Cloud控制台正确获取的:- 必须匹配你的应用包名(Android)或Bundle ID(iOS)
- 确认已在控制台启用Google登录API
- 如果是跨平台应用,除了Web客户端ID,还要配置对应平台的Android/iOS客户端ID
暂时关闭离线访问测试
如果不需要离线获取用户数据,先把offlineAccess设为false,开启离线访问需要额外的权限配置,先排除这个因素:GoogleSignin.configure({ webClientId: 'XXX', offlineAccess: false, // 先改为false测试 forceCodeForRefreshToken: true, });打印完整错误信息
在catch块里输出完整错误对象,而不只是message,能获取更详细的失败原因:console.log('完整错误:', JSON.stringify(error, null, 2));检查Android签名证书SHA-1
在Google Cloud控制台,你的Android客户端ID必须绑定应用的签名证书SHA-1,debug和release版本的SHA-1都要分别添加,否则会触发登录失败。iOS平台补充配置
如果是iOS应用:- 在
Info.plist中添加Google登录的URL Scheme,格式为com.googleusercontent.apps.你的客户端ID - 启用Xcode项目的Keychain Sharing权限
- 确认Apple开发者后台已配置对应的应用权限
- 在
补充缺失的函数实现
代码中getCurrentUserInfo函数未定义,需要补充实现才能获取已登录用户的信息:const getCurrentUserInfo = async () => { try { const userInfo = await GoogleSignin.getCurrentUser(); setUser(userInfo); console.log('当前用户信息:', userInfo); } catch (error) { console.log('获取用户信息失败:', error); } };
内容的提问来源于stack exchange,提问作者Piyush Chatterjee
相关产品推荐
相关产品推荐

