Serverpod集成Google登录失败:连接超时及认证问题求助
Serverpod + Flutter Google登录无法正常运行问题
错误日志
Syncing files to device sdk gphone x86... D/ProfileInstaller(20285): Installing profile for com.kaaljit.talkinbird I/flutter (20285): Failed call: serverpod_auth.status.getUserInfo I/flutter (20285): SocketException: HTTP connection timed out after 0:00:20.000000, host: <My IP Address>, port: 8080 I/flutter (20285): Failed call: user.getUser I/flutter (20285): SocketException: HTTP connection timed out after 0:00:20.000000, host: <My IP Address>, port: 8080 [log] SocketException: HTTP connection timed out after 0:00:20.000000, host: <My IP Address>, port: 8080 I/flutter (20285): serverpod_auth_google: GoogleSignIn I/flutter (20285): serverpod_auth_google: Signing out from google I/flutter (20285): serverpod_auth_google: PlatformException(status, Failed to disconnect., null, null) I/flutter (20285): Failed call: serverpod_auth.google.authenticateWithServerAuthCode I/flutter (20285): SocketException: HTTP connection timed out after 0:00:20.000000, host: <My IP Address>, port: 8080 I/flutter (20285): serverpod_auth_google: SocketException: HTTP connection timed out after 0:00:20.000000, host: <My IP Address>, port: 8080 I/flutter (20285): #0 _ConnectionTarget.connect.<anonymous closure>.<anonymous closure> (dart:_http/http_impl.dart:2530:11) I/flutter (20285): #1 _FutureListener.handleError (dart:async/future_impl.dart:180:22) I/flutter (20285): #2 Future._propagateToListeners.handleError (dart:async/future_impl.dart:850:47) I/flutter (20285): #3 Future._propagateToListeners (dart:async/future_impl.dart:871:13) I/flutter (20285): #4 Future._completeError (dart:async/future_impl.dart:651:5) I/flutter (20285): #5 Future.timeout.<anonymous closure> (dart:async/future_impl.dart:923:17) I/flutter (20285): #6 Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15) I/flutter (20285): #7 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:398:19) I/flutter (20285): #8 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:429:5) I/flutter (20285): #9 _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
核心代码
main.dart
late SessionManager sessionManager; late Client client; void main() async { WidgetsFlutterBinding.ensureInitialized(); const ipAddress = <My IP Address>; // const ipAddress = '10.0.0.2'; client = Client( 'http://$ipAddress:8080/', authenticationKeyManager: FlutterAuthenticationKeyManager(), )..connectivityMonitor = FlutterConnectivityMonitor(); sessionManager = SessionManager( caller: client.modules.auth, ); await sessionManager.initialize(); runApp(const TalkinBird()); }
登录页面代码
import 'package:flutter/material.dart'; import 'package:serverpod_auth_google_flutter/serverpod_auth_google_flutter.dart'; import 'package:talkinbird_flutter/screens/user_details_ui.dart'; import '../main.dart'; const _serverClientId = "<Web Application's Client ID>"; class SignInPage extends StatelessWidget { const SignInPage({super.key}); @override Widget build(BuildContext context) { return Scaffold( body: Center( child: Dialog( child: Container( width: 260, padding: const EdgeInsets.all(16), child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ SignInWithGoogleButton( onSignedIn: () { print('Signed in with Google!'); Navigator.of(context).push(MaterialPageRoute( builder: (context) => const UserDetailsUI(), )); }, caller: client.modules.auth, serverClientId: _clientId, redirectUri: Uri.parse('http://localhost:8082/googlesignin'), ), ], ), ), ), ), ); } }
环境信息
- Serverpod版本:CLI 1.2.6,库1.2.6
- 开发环境:Windows设备,Android Studio,Pixel 8 API 30安卓模拟器
- Gradle配置:
org.gradle.jvmargs=-XX:MaxHeapSize=256m -Xmx2536m,已启用multidex;compileSdk 34,ndkVersion "25.1.8937393",minSdkVersion为flutter.minSdkVersion,targetSdkVersion为flutter.targetSdkVersion - Flutter版本:Channel stable 3.19.4,
flutter doctor无异常
已执行操作
- 服务端配置:添加
serverpod_auth_server:1.2.6,在server.dart中注册Google登录路由/googlesignin,执行serverpod generate、迁移并启动服务 - 客户端配置:添加
serverpod_auth_client:1.2.6,Flutter端添加serverpod_auth_shared_flutter:1.2.6和serverpod_auth_google_flutter:1.2.6 - GCP配置:创建项目并启用People API,生成Web应用客户端ID(配置授权源和重定向URI),下载JSON保存为
server/config/google_client_secret.json;生成安卓客户端ID(配置SHA-1),下载JSON保存为android/app/google-services.json - 调试尝试:使用本机IP和
10.0.0.2连接,尝试切换Web/安卓客户端ID,执行./gradlew clean、flutter clean、flutter pub cache repair及Android Studio缓存清理重启
预期行为
点击Google登录按钮完成授权后,触发onSignedIn回调,跳转到UserDetailsUI页面
解决方案
1. 修复模拟器与服务端的网络连通问题
Windows安卓模拟器访问本地服务需用10.0.2.2(模拟器映射主机localhost的默认地址),修改main.dart中的IP配置:
const ipAddress = '10.0.2.2';
同时确保Serverpod服务监听所有网卡,在config/config.yaml中修改:
webServer: host: 0.0.0.0 port: 8080
2. 修正客户端ID变量名错误
登录页面代码中定义了_serverClientId,但使用时误写为_clientId,修改为:
SignInWithGoogleButton( // ...其他参数 serverClientId: _serverClientId, // ... )
3. 调整OAuth重定向URI
在GCP Web客户端ID的重定向URI中添加:http://10.0.2.2:8082/googlesignin
确保Serverpod服务的google_client_secret.json配置正确,且/googlesignin路由已注册。
4. 验证服务端状态
启动Serverpod后,先在主机浏览器访问http://localhost:8080/health确认服务正常;再在模拟器浏览器访问http://10.0.2.2:8080/health,验证网络连通性。
5. 处理登出异常
若仍出现Failed to disconnect异常,可简化登录逻辑,跳过不必要的自动登出操作,或确保Google Sign-In状态管理流程正确。
内容的提问来源于stack exchange,提问作者Aditya Dwivedi
相关产品推荐
相关产品推荐

