Flutter Web Google 登录

Ste*_*in. 7 google-authentication firebase-authentication flutter flutter-web hummingbird

我尝试在 Flutter Web 中使用 Google 实现登录。我使用GoogleSignn 4.1.1Firebase Auth 0.15.4。我没有收到任何错误消息。它只是不弹出。

  • 我在 Firebase(添加的依赖项)中注册了 Web 应用程序,甚至添加了<meta>带有google-signin-client_id
  • 当我在 Android 上运行 Firebase Auth with Google 时它可以工作
  • 我还在Web 中从 GoogleSignIn运行了示例应用程序。它也不会弹出。

这是我的登录代码(适用于 Android)

    final FirebaseAuth _auth = FirebaseAuth.instance;
    FirebaseUser user = await _auth.currentUser();
    if (user != null) {
      log.d('alreadyLoggedIn');
    } else {
      final GoogleSignIn _googleSignIn = GoogleSignIn(clientId: Constants.GOOGLE_SIGN_IN_CLIENT_ID);
      final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
      final GoogleSignInAuthentication googleAuth =
          await googleUser.authentication;
      final AuthCredential credential = GoogleAuthProvider.getCredential(
        accessToken: googleAuth.accessToken,
        idToken: googleAuth.idToken,
      );
      await _auth.signInWithCredential(credential);
      user = await _auth.currentUser();
      assert(user.email != null);
      assert(user.displayName != null);
      assert(!user.isAnonymous);
      assert(await user.getIdToken() != null);
    }
    return user;
    }
Run Code Online (Sandbox Code Playgroud)

我希望有人知道如何解决这个问题。

Chr*_*ris 8

您是否遵循了此页面中的所有说明(包括将 OAuth ID 添加到 index.html)? https://pub.dev/packages/google_sign_in_web

您从https://console.developers.google.com/apis/credentials获取您的客户 ID

您还必须像这样从终端运行,以便在调试时在本地主机上运行:

flutter run -d chrome --web-hostname localhost --web-port 5000

默认授权端口为 5000,您可以在获得 CLIENT ID 的同一页面上添加其他 URI(例如8764367243864-987523.apps.googleusercontent.com),它位于“Authorised JavaScript origins” https://console.developers.google 下。 com/apis/凭证