Flutter 应用程序无法使用 google_sign_in 包对 Google Drive API 进行身份验证

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

我的 Flutter 应用程序(Android 版本)无法使用 google_sign_in 包(版本 5.4.2)以及 extension_google_sign_in_as_googleapis_auth 包(版本 2.0.7)对 Google Drive API 进行身份验证。

我得到的确切错误是:

PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException:10:,null, null’)’

我没有使用 Firebase,因此所有 Google API 配置项都是通过 Google Cloud Console 完成的,而不是 Firebase 控制台,通常情况下,上一篇文章会出现与我相同的错误。

我的盒子是 Mac Air M1,由于我的 Flutter 应用程序是 Android 变体,并且 M1 仍然没有 Android 模拟器,因此我的调试是通过通过 USB C 电缆连接到计算机的真实 Android 设备完成的。

代码片段如下:

Future<http.Client> getHttpClientFromGoogle() async {
    
   String clientIdOAuth = <from client id string in Google Cloud Console (see below)>;
        
           

   List scopes = [ 'https://www.googleapis.com/auth/drive.file' ];

   GoogleSignIn signIn = GoogleSignIn(clientId: clientIdOAuth,scopes:scopes);

    signIn.onCurrentUserChanged.listen(
      (user) {
        print(user);
      },
    );

    var _account = await signIn.signIn();    <— Exception happens here.

    <more code here>
}

当我尝试登录时出现问题,导致出现同意书。我选择指定测试人员的电子邮件后抛出异常。

即- enter image description here

flutter doctor 输出如下:

enter image description here

这就是我在 Google Cloud Console 中所做的事情:

  • 我创建了一个 Google Cloud 项目

  • API 和服务 > 启用的 API 和服务页面下,我启用了 Google Drive APIPeople API。u2028

  • API 和服务 > 凭据页面下,我点击创建凭据,通过注册具有以下特征的应用程序来创建 OAuth 客户端 ID

  • 应用程序类型:Android

  • 包名称:<— set to value of the /android/app/src/main/AndroidManifest.xmlmanifest标签中的package

    属性
  • SHA1 证书指纹:<— set to the SHA1 specified in the app signing report generated by running ./gradlew signingReport in the Android Studio terminal. NOTE: I also verified that this SHA1 is in my debug.keystore 文件。

  • API 和服务 > OAuth 同意屏幕 页面下,我验证了以下内容:

  • 我的发布状态:测试中

  • 用户类型:外部

  • 然后我点击编辑应用程序。u2028

  • 在结果页面中,我点击保存并继续进入范围页面,我在其中指定使用 Google Drive API 时所需的范围。u2028

  • 我再次点击保存并继续进入指定允许测试应用程序的用户电子邮件的页面。u2028

  • 测试用户:<— Set to the email of the users allowed to test the app.

对于这么长的帖子,我深表歉意。只是我已经这样做了一段时间,尝试了以前帖子中的所有建议,但都无济于事。另外,我曾经使用 googleapis_auth 包通过 Google API 进行身份验证没有任何问题。只是当 Google 禁用了 googleapis_auth 所依赖的 loopback flow 时,建议我切换到 google_sign_in

预先感谢您的任何建议。

/何塞

android flutter google-drive-api google-signin
2个回答
1
投票

使用包 google_sign_in:^5.4.2

  Future<void> gmailSignIn(BuildContext context) async {
    try {
      final GoogleSignIn googleSignIn = GoogleSignIn();
      final GoogleSignInAccount? googleSignInAccount = await googleSignIn.signIn();
      if (googleSignInAccount != null) {
        final GoogleSignInAuthentication googleSignInAuthentication =
        await googleSignInAccount.authentication;
        print("idToken = ${googleSignInAuthentication.idToken}");
        final AuthCredential authCredential = GoogleAuthProvider.credential(idToken: googleSignInAuthentication.idToken, accessToken: googleSignInAuthentication.accessToken);
        print("accessToken = ${authCredential.accessToken}");
        // Getting users credential
        UserCredential result = await auth.signInWithCredential(authCredential);
        User? user = result.user;
        print("user details = ${user.toString()}");

   
      }
    } on FirebaseAuthException catch (e) {
      print("Error on google sign in");
      print(e.message);
      rethrow;
    } catch (e) {
      print("Error hai");
      print(e);
      rethrow;
    } finally {
     
    }    
  }

0
投票

我也因类似的错误而苦苦挣扎。 我试图根据这篇post的工作代码(更新3)构建一个YouTube视频上传器应用程序,然后我遇到了

PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException:10:,null, null

如果您没有使用 firebase 并且问题发生在 android 上,那么您必须按照此 blog 的第 4 步进行操作,这些是 -

  1. 启用您感兴趣的 Google 云 api,并按照 OP 的说明创建 OAuth 凭证。

2.进入android/app中的build.gradle,再次找到依赖项。然后,添加这行代码。您可能需要更新

play-services-auth
库的版本。

dependencies { implementation 'com.google.android.gms:play-services-auth:20.4.1' } 

  1. 仅通过传递范围来创建 GoogleSignIn 对象。 不要在此处传递 clientId

     GoogleSignIn _googleSignIn = GoogleSignIn( scopes: <String>[YT.YouTubeApi.youtubeReadonlyScope, YT.YouTubeApi.youtubeUploadScope], );

  2. 更新 google_sign_in 库的 pubsec.yaml。

如前所述,我必须只更新 app/build.gradle ,而不是更新 classpath 'com.google.gms:google-services:4.3.15'

 的 root
级别 build.gradle,如上述媒体博客中所述。 Infcat,我的样本中没有这样的块。

© www.soinside.com 2019 - 2024. All rights reserved.