我正在尝试使用 Serverpod 和 Flutter 实现 Google Sign,但它不起作用..
应用程序在 Android 模拟器上启动后立即出现错误日志 -
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 版本
平台信息 我在 Windows 机器上使用 Android Studio 并在 Pixel 8 API 30 Android 模拟器上工作。 我已在
org.gradle.jvmargs=-XX:MaxHeapSize=256m -Xmx2536m
中定义了 gradle.properties
并启用了 multidex 支持。
在我的应用程序/gradle中-compileSdk是34,ndkVersion“25.1.8937393”,minSdkVersion是flutter.minSdkVersion
,targetSdkVersion是flutter.targetSdkVersion
这是扑医生的失败 -
PS C:\talkinbird\talkinbird_flutter> flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.19.4, on Microsoft Windows [Version 10.0.22621.3296], locale en-IN)
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[√] Chrome - develop for the web
[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.7.6)
[√] Android Studio (version 2023.2)
[√] Connected device (4 available)
[√] Network resources
• No issues found!
我在做什么
serverpod_auth_server: 1.2.6
,在 server.dart 中添加了pod.webServer.addRoute(auth.RouteGoogleSignIn(), '/googlesignin');
,并运行了 serverpod 生成、应用了迁移并运行了 Web 服务器。modules: serverpod_auth: nickname: auth
serverpod_auth_client: 1.2.6
serverpod_auth_shared_flutter: 1.2.6
和 serverpod_auth_google_flutter: 1.2.6
。http://localhost:8082
和 http://localhost:49660
。将授权重定向 URI 定义为 http://localhost:8082
、http://localhost:49660
、http://localhost:8082/googlesignin
。server/config/google_client_secret.json
。talkinbird_flutter/android/app/google-services.json
.flutter clean
和flutter pub cache repair
以及无效缓存并重新启动,同时尝试上述各种解决方案。预期行为 onSignedIn 回调被执行,导航器将我带到定义的下一个屏幕。
我不太确定这是否是我所面临的类似错误,因为我在撰写本文时正在使用最新的(2.0.1)serverpod 和所有与 serverpod 相关的软件包。他们更优雅地处理错误,现在错误日志有点不同。
我还有一些误导性的日志。事实证明,如果您遵循了 serverpod 的 google auth 指南,他们就会跳过设置的重要部分。
这就是配置重定向uri。根据 this issues,如果 json 文件中缺少
google_client_secret.json
键,则无法读取 redirect_uris
。
对我来说,解决方法是转到 google 云控制台,编辑 Serverpod 服务器的凭据,然后添加“授权重定向 URI”以匹配我在通话中使用的凭据。然后我再次下载了 json 文件,将其重命名为
google_client_secret.json
,突然之间,一切正常了。只需检查您的 google_client_secret.json
是否包含以下内容:
{
"web": {
...
"redirect_uris": [
"http://localhost:8082/googlesignin"
]
}
}