Flutter Google Sign-in,选择帐户后卡住

mik*_*ssy 1 firebase firebase-authentication google-signin flutter

我在尝试在 Flutter 应用程序中使用 Google Sign-in 登录 Firebase 时遇到问题。

我看过另外两篇关于此问题的帖子,但没有答案可以为我自己解决这个问题。我在 flutter 中使用 Google Sign-in 提供程序,google_sign_in: ^5.1.0,Flutter 版本 2.2.1

我已按照有关生成/添加 Sha-1、Sha-256 密钥到我的 firebase 项目的说明进行操作。在此之前,我遇到了 API 异常。我添加了密钥,将最新的 google-json 文件下载到我的 android 文件夹中,并克服了该错误。现在,当我单击“登录”时,我会看到帐户选择屏幕。

在此输入图像描述

我选择了我的帐户,然后我点击了一个带有无尽旋转的白色页面。

在此输入图像描述

这是我正在运行的登录代码。

GoogleSignIn _googleSignIn = GoogleSignIn(
  scopes: <String>[
    'email',
    'https://www.googleapis.com/auth/contacts.readonly',
  ],
);

  Future<void> _handleGoogleSignIn() async {
    try {
      GoogleSignInAccount? googleSignInAccount =
          await _googleSignIn.signIn();

      GoogleSignInAuthentication? googleSignInAuthentication =
          await googleSignInAccount?.authentication;
    

      final AuthCredential credential = GoogleAuthProvider.credential(
        accessToken: googleSignInAuthentication?.accessToken,
        idToken: googleSignInAuthentication?.idToken,
      );

      final UserCredential user = await _auth.signInWithCredential(credential);  
    } catch (error) {
      print('in error block');
      print(error);
    }
  }
Run Code Online (Sandbox Code Playgroud)

在进行一些调试时,它似乎没有超出初始行:

 GoogleSignInAccount? googleSignInAccount =
          await _googleSignIn.signIn();
Run Code Online (Sandbox Code Playgroud)

但我没有收到任何错误,任何警告,什么都没有......只是一个无限期地坐着的旋转器。关于如何解决这个问题,或者至少调试这个问题而不出现错误,有什么想法吗?谢谢

mik*_*ssy 5

经过进一步挖掘,我发现他对 Flutter github 帐户上报告的同一错误进行了回答。

https://github.com/flutter/flutter/issues/89169

解决方案:

在以下代码片段中(定义为默认设置)

GoogleSignIn _googleSignIn = GoogleSignIn(
  scopes: [
    'email',
    'https://www.googleapis.com/auth/contacts.readonly',
  ],
);
Run Code Online (Sandbox Code Playgroud)

将https://www.googleapis.com/auth/contacts.readonly (受限范围)更改为https://www.googleapis.com/auth/userinfo.profile(非受限范围)。

之后,OAuth 屏幕将继续登录。