未处理的异常:PlatformException (sign_in_failed, com.google.android.gms.common.api.ApiException: 10: , null, null) - 在一个系统中运行良好

Kat*_*van 5 java android firebase firebase-authentication flutter

我正在使用 flutter 中的平台通道(使用 Java)开发蓝牙应用程序。但是当我尝试使用 google-sign in( google_sign_in: ^4.5.6 ) 登录时,我收到错误。\n我可以登录详细信息,但无法移动到下一页......\n实际上,它在我的办公系统中工作,但是当我复制该项目并在另一个系统中运行时,它给出了错误......任何人都可以帮忙吗

\n
main.dart\nimport 'package:bmsapp2/pages/loginpage.dart';\nimport 'package:flutter/material.dart';\n\nvoid main() {\n  runApp(BmsApp());\n}\n\nclass BmsApp extends StatelessWidget {\n  @override\n  Widget build(BuildContext context) {\n    const curveHeight = 12.0;\n    return MaterialApp(\n      home: SafeArea(\n        child: Scaffold(\n          appBar: AppBar(\n            backgroundColor: Colors.amber[900],\n            shape: const MyShapeBorder(curveHeight),\n          ),\n          body: LoginPage(),\n        ),\n      ),\n    );\n  }\n}\n\nclass MyShapeBorder extends ContinuousRectangleBorder {\n  const MyShapeBorder(this.curveHeight);\n  final double curveHeight;\n\n  @override\n  Path getOuterPath(Rect rect, {TextDirection textDirection}) => Path()\n    ..lineTo(0, rect.size.height)\n    ..quadraticBezierTo(\n      rect.size.width / 2,\n      rect.size.height + curveHeight * 2,\n      rect.size.width,\n      rect.size.height,\n    )\n    ..lineTo(rect.size.width, 0)\n    ..close();\n}\n\n
Run Code Online (Sandbox Code Playgroud)\n
loginpage.dart\nimport 'package:flutter/material.dart';\nimport 'package:google_sign_in/google_sign_in.dart';\nimport 'package:avatar_glow/avatar_glow.dart';\n\nimport 'bluetoothpage.dart';\n\nfinal GoogleSignIn googleSignIn = GoogleSignIn(scopes: ['profile', 'email']);\n\nclass LoginPage extends StatefulWidget {\n  LoginPage({Key key}) : super(key: key);\n\n  @override\n  _LoginPageState createState() => _LoginPageState();\n}\n\nclass _LoginPageState extends State<LoginPage> {\n  bool isAuth = false;\n  GoogleSignInAccount _currentUser;\n\n  Widget buildAuthScreen() {\n    //return Text(_currentUser.displayName ?? '');\n    return SafeArea(\n      child: Center(\n        child: Container(\n          margin: EdgeInsets.only(top: 50),\n          child: Column(\n            //mainAxisAlignment: MainAxisAlignment.center,\n            children: [\n              CircleAvatar(\n                backgroundColor: Colors.white,\n                backgroundImage: NetworkImage(_currentUser.photoUrl),\n                radius: 50,\n              ),\n              SizedBox(height: 15),\n              Text(\n                'You are logged in as ',\n                style: TextStyle(\n                  color: Colors.grey,\n                  fontSize: 15,\n                ),\n              ),\n              SizedBox(height: 5),\n              Text(\n                _currentUser.email,\n                style: TextStyle(\n                  color: Colors.black,\n                  fontSize: 15,\n                  fontWeight: FontWeight.bold,\n                ),\n              ),\n              ElevatedButton(\n                onPressed: _logout,\n                child: Text("Logout".toUpperCase(),\n                    style: TextStyle(fontSize: 14, letterSpacing: 2)),\n                style: ButtonStyle(\n                    foregroundColor:\n                        MaterialStateProperty.all<Color>(Colors.white),\n                    backgroundColor:\n                        MaterialStateProperty.all<Color>(Colors.amber[900]),\n                    shape: MaterialStateProperty.all<RoundedRectangleBorder>(\n                        RoundedRectangleBorder(\n                            borderRadius: BorderRadius.circular(5),\n                            side: BorderSide(color: Colors.amber[900])))),\n              ),\n              Container(\n                // height: 300,\n                child: AvatarGlow(\n                  glowColor: Colors.blue,\n                  endRadius: 70.0,\n                  duration: Duration(milliseconds: 2000),\n                  repeat: true,\n                  showTwoGlows: true,\n                  repeatPauseDuration: Duration(milliseconds: 100),\n                  child: Material(\n                    elevation: 4.0,\n                    shape: CircleBorder(),\n                    child: CircleAvatar(\n                      backgroundColor: Colors.grey[200],\n                      child: Image.asset(\n                        'images/bt.png',\n                        height: 40,\n                        width: 250,\n                        fit: BoxFit.fitWidth,\n                      ),\n                      radius: 40.0,\n                    ),\n                  ),\n                ),\n              ),\n              ElevatedButton(\n                onPressed: () {\n                  Navigator.push(context,\n                      MaterialPageRoute(builder: (context) => BluetoothPage()));\n                },\n                child: Text('Find your device',\n                    style: TextStyle(fontSize: 15, letterSpacing: 2)),\n                style: ButtonStyle(\n                    foregroundColor:\n                        MaterialStateProperty.all<Color>(Colors.white),\n                    backgroundColor:\n                        MaterialStateProperty.all<Color>(Colors.amber[900]),\n                    shape: MaterialStateProperty.all<RoundedRectangleBorder>(\n                        RoundedRectangleBorder(\n                            borderRadius: BorderRadius.circular(5),\n                            side: BorderSide(color: Colors.amber[900])))),\n              ),\n            ],\n          ),\n        ),\n      ),\n    );\n  }\n\n  _login() {\n    googleSignIn.signIn();\n  }\n\n  _logout() {\n    googleSignIn.signOut();\n  }\n\n  Widget buildUnAuthScreen() {\n    return Scaffold(\n        body: Center(\n      child: Column(\n        mainAxisAlignment: MainAxisAlignment.center,\n        crossAxisAlignment: CrossAxisAlignment.center,\n        children: [\n          Container(\n            child: Image.asset('images/touch.png'),\n          ),\n          SizedBox(\n            height: 5.0,\n          ),\n          Text(\n            'Next Generation Battery',\n            style: TextStyle(fontSize: 15, color: Colors.grey),\n          ),\n          SizedBox(\n            height: 5.0,\n          ),\n          Container(\n            child: GestureDetector(\n              onTap: () {\n                _login();\n              },\n              child: Image.asset(\n                'images/signin.png',\n                height: 75,\n                width: 250,\n                fit: BoxFit.fitWidth,\n              ),\n            ),\n          ),\n          Text(\n            'Sign up here',\n            style: TextStyle(fontSize: 15, color: Colors.grey),\n          ),\n        ],\n      ),\n    ));\n  }\n\n  void handleSignin(GoogleSignInAccount account) {\n    if (account != null) {\n      print('User Signed in $account');\n\n      setState(() {\n        isAuth = true;\n        _currentUser = account;\n      });\n    } else {\n      setState(() {\n        isAuth = false;\n        //_currentUser = null;\n      });\n    }\n  }\n\n  @override\n  void initState() {\n    super.initState();\n\n    googleSignIn.onCurrentUserChanged.listen((GoogleSignInAccount account) {\n      handleSignin(account);\n    }, onError: (err) {\n      print('Error Signiing in : $err');\n    });\n    // Reauthenticate user when app is opened\n    /*googleSignIn.signInSilently(suppressErrors: false).then((account) {\n      handleSignin(account);\n    }).catchError((err) {\n      print('Error Signiing in : $err');\n    });*/\n  }\n\n  @override\n  Widget build(BuildContext context) {\n    return isAuth ? buildAuthScreen() : buildUnAuthScreen();\n  }\n}\n\n
Run Code Online (Sandbox Code Playgroud)\n
error:\nE/flutter (12476): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 10: , null, null)\nE/flutter (12476): #0      StandardMethodCodec.decodeEnvelope\npackage:flutter/\xe2\x80\xa6/services/message_codecs.dart:581\nE/flutter (12476): #1      MethodChannel._invokeMethod\npackage:flutter/\xe2\x80\xa6/services/platform_channel.dart:158\nE/flutter (12476): <asynchronous suspension>\nE/flutter (12476): #2      MethodChannel.invokeMapMethod\npackage:flutter/\xe2\x80\xa6/services/platform_channel.dart:358\nE/flutter (12476): <asynchronous suspension>\nE/flutter (12476):\nD/ViewRootImpl@4370975[SignInHubActivity](12476): mHardwareRenderer.destroy()#1\nD/ViewRootImpl@4370975[SignInHubActivity](12476): Relayout returned: oldFrame=[0,0][1440,2560] newFrame=[0,0][1440,2560] result=0x5 surface={isValid=false 0} surfaceGenerationChanged=true\nD/ViewRootImpl@4370975[SignInHubActivity](12476): mHardwareRenderer.destroy()#4\nD/ViewRootImpl@4370975[SignInHubActivity](12476): dispatchDetachedFromWindow\nD/InputTransport(12476): Input channel destroyed: fd=102\n\n
Run Code Online (Sandbox Code Playgroud)\n

Hut*_*yad 9

[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 10: , null, null)apiException:10 表示这很可能是由于 SHA-1 或 SHA-256 设置不正确造成的。既然您说它在办公室工作,那么它可能与您现在使用的环境不同,您应该添加这些键。这还假设您正在运行调试版本,因此请添加调试 SHA-1 密钥。


Kai*_*nov 6

从 Google Play 下载的应用程序也会出现此问题。这是因为 Google 使用自己的密钥对您的应用程序进行签名,而您无法访问该密钥。您可以从以下位置获取它的 SHA1:Google Play Console -> 您的应用程序 -> 设置 -> 应用程序签名

  • 这正是我的问题。我需要从 google 获取 sha1 来添加到 firebase 项目设置中。要么缺少文档,要么我只是盲目的:)非常感谢你 - 它结束了大约 2 小时的冒险之旅:) (3认同)

小智 6

最近经过多次尝试修复了这个问题,在更新到 flutter-3.1.0-pre9 并将 Android studio 更新到chipmunk 2021.2.1 后,我收到了一条有趣的调试消息。它google sign in在 iOS 上运行良好,但无论我做什么,我总是收到这个确切的消息PlatformException (sign_in_failed... blah blah blah

新的错误消息现在也包含以下内容:clientId is not supported on Android and is interpreted as serverClientId. Use serverClientId

在Android上添加clientId为参数似乎是问题

确保您的密钥库sha-1已在 firebase 上注册,并且您已完成 firebase 上所述的说明

late GoogleSignIn googleSignIn;
***
@override
void initState() {
 ***
 googleSignIn = GoogleSignIn(
        scopes: [
          'email',
          'https://www.googleapis.com/auth/userinfo.profile',
        ],
        serverClientId: isIOS ? null : googleClientId,
        clientId: isIOS ? googleClientIdIOS : null);
 ***
Run Code Online (Sandbox Code Playgroud)

您可以通过这种方式clientId在 iOS 和serverClientIdAndroid 上进行设置。希望这有帮助。