Serverpod 的 Google 登录给我 - 调用失败:serverpod_auth.google.authenticateWithServerAuthCode

问题描述 投票:0回答:1

我正在尝试使用 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 版本

  • CLI 版本: 1.2.6
  • 库版本:1.2.6

平台信息 我在 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!

我在做什么

  1. 我在服务器的 pubspec 中添加了
      serverpod_auth_server: 1.2.6
    ,在 server.dart 中添加了
    pod.webServer.addRoute(auth.RouteGoogleSignIn(), '/googlesignin');
    ,并运行了 serverpod 生成、应用了迁移并运行了 Web 服务器。
  2. 我已经在generator.yaml中定义了
    modules: serverpod_auth: nickname: auth
  3. 我在客户的 pubspec 中添加了
    serverpod_auth_client: 1.2.6
  4. 我在 Flutter App 的 pubspec 中添加了
    serverpod_auth_shared_flutter: 1.2.6
    serverpod_auth_google_flutter: 1.2.6
  5. 我已在 GCP 中创建了一个项目并启用了 People API
  6. 我已经为 Web 应用程序创建了客户端 ID - 将授权 JavaScript 来源定义为
    http://localhost:8082
    http://localhost:49660
    。将授权重定向 URI 定义为
    http://localhost:8082
    http://localhost:49660
    http://localhost:8082/googlesignin
  7. 我已下载 Web 应用程序 JSON 并将其存储为
    server/config/google_client_secret.json
  8. 我已经为 Android 创建了客户端 ID 并定义了 SHA-1 证书指纹。我已经下载了 Json 文件并将其存储为
    talkinbird_flutter/android/app/google-services.json
    .
  9. 我正在模拟器上进行测试,因此我的 PC 和 AVD 都具有相同的 IP。
  10. 我尝试使用我自己的 IP 地址和 10.0.0.2 - 使用我自己的 IP 地址会将我带到 OAuth 同意屏幕,然后给我错误,点击我的 gmail id 后立即使用 10.0.0.2 给我错误。
  11. 我尝试在 SignInWithGoogleButton 中使用 Android 的客户端 ID 和 Web 应用程序的客户端 ID,发现 Web 应用程序会将我带到 OAuth 同意屏幕,所以我相信它是正确的使用方式。
  12. 我尝试在 Android Studio 中多次运行“./gradlew clean”、
    flutter clean
    flutter pub cache repair
    以及无效缓存并重新启动,同时尝试上述各种解决方案。

预期行为 onSignedIn 回调被执行,导航器将我带到定义的下一个屏幕。

flutter dart google-cloud-platform google-oauth serverpod
1个回答
0
投票

我不太确定这是否是我所面临的类似错误,因为我在撰写本文时正在使用最新的(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"
        ]
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.