Firebase 颤振未处理异常:PlatformException(登录失败,com.google.android.gms.common.api.ApiException:10:,null)

Firebase 颤振未处理异常:PlatformException(登录失败,com.google.android.gms.common.api.ApiException:10:,null),firebase,flutter,sha,Firebase,Flutter,Sha,我想使用谷歌帐户登录我的应用程序,但我得到了 错误:flatter/lib/ui/ui\u dart\u state.cc(157)]未处理的异常:PlatformException(登录失败,com.google.android.gms.common.api.apiexeption:10:,null) main.dart: import 'package:flutter/cupertino.dart'; import 'package:flutter/material.

我想使用谷歌帐户登录我的应用程序,但我得到了

错误:flatter/lib/ui/ui\u dart\u state.cc(157)]未处理的异常:PlatformException(登录失败,com.google.android.gms.common.api.apiexeption:10:,null)

main.dart:

      import 'package:flutter/cupertino.dart';
      import 'package:flutter/material.dart';
      import 'package:flutter_auth_buttons/flutter_auth_buttons.dart';


      import 'package:firebase_auth/firebase_auth.dart';
      import 'package:google_sign_in/google_sign_in.dart';


      void main() => runApp(MyApp());

      class MyApp extends StatelessWidget {
        // This widget is the root of your application.
        @override
        Widget build(BuildContext context) {
          return MaterialApp(
            title: 'Flutter Demo',
            theme: ThemeData(
              // This is the theme of your application.
              //
              // Try running your application with "flutter run". You'll see the
              // application has a blue toolbar. Then, without quitting the app, try
              // changing the primarySwatch below to Colors.green and then invoke
              // "hot reload" (press "r" in the console where you ran "flutter run",
              // or simply save your changes to "hot reload" in a Flutter IDE).
              // Notice that the counter didn't reset back to zero; the application
              // is not restarted.
              primarySwatch: Colors.blue,
            ),
            home: MyHomePage(),
          );
        }
      }

      class MyHomePage extends StatefulWidget {
        MyHomePage({Key key}) : super(key: key);

        // This widget is the home page of your application. It is stateful, meaning
        // that it has a State object (defined below) that contains fields that affect
        // how it looks.

        // This class is the configuration for the state. It holds the values (in this
        // case the title) provided by the parent (in this case the App widget) and
        // used by the build method of the State. Fields in a Widget subclass are
        // always marked "final".


        @override
        _MyHomePageState createState() => _MyHomePageState();
      }

      class _MyHomePageState extends State<MyHomePage> {

        bool _value=false;
        final FirebaseAuth _auth = FirebaseAuth.instance;
        final GoogleSignIn googleSignIn = GoogleSignIn();
        Future<String> signInWithGoogle() async {
          final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
          final GoogleSignInAuthentication googleSignInAuthentication =
          await googleSignInAccount.authentication;

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

          final AuthResult authResult = await _auth.signInWithCredential(credential);
          final FirebaseUser user = authResult.user;

          assert(!user.isAnonymous);
          assert(await user.getIdToken() != null);

          final FirebaseUser currentUser = await _auth.currentUser();
          assert(user.uid == currentUser.uid);

          return 'signInWithGoogle succeeded: $user';
        }

        void signOutGoogle() async{
          await googleSignIn.signOut();

          print("User Sign Out");
        }


        @override
        Widget build(BuildContext context) {
          // This method is rerun every time setState is called, for instance as done
          // by the _incrementCounter method above.
          //
          // The Flutter framework has been optimized to make rerunning build methods
          // fast, so that you can just rebuild anything that needs updating rather
          // than having to individually change instances of widgets.
          return Scaffold(

              body:
              Center(
                  child:


                  GoogleSignInButton(
                    onPressed: () {
                      signInWithGoogle().whenComplete(() {
                        Navigator.of(context).push(
                          MaterialPageRoute(
                            builder: (context) {
                              return FirstScreen();
                            },
                          ),
                        );
                      });
                    },
                    darkMode: true, // default: false
                  )
              )


            // This trailing comma makes auto-formatting nicer for build methods.
          );
        }
      }

      class FirstScreen extends StatelessWidget {
        @override
        Widget build(BuildContext context) {
          return Scaffold(
            body: Container(color: Colors.blue[100]),
          );
        }
      }
app/build.gradle

        def localProperties = new Properties()
        def localPropertiesFile = rootProject.file('local.properties')
        if (localPropertiesFile.exists()) {
            localPropertiesFile.withReader('UTF-8') { reader ->
                localProperties.load(reader)
            }
        }

        def flutterRoot = localProperties.getProperty('flutter.sdk')
        if (flutterRoot == null) {
            throw new GradleException("Flutter SDK not found. Define location with 
        flutter.sdk in the local.properties file.")
        }

        def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
        if (flutterVersionCode == null) {
            flutterVersionCode = '1'
        }

        def flutterVersionName = localProperties.getProperty('flutter.versionName')
        if (flutterVersionName == null) {
            flutterVersionName = '1.0'
        }

        apply plugin: 'com.android.application'
        apply plugin: 'kotlin-android'
        apply plugin: 'com.google.gms.google-services'
        apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
         def keystoreProperties = new Properties()
           def keystorePropertiesFile = rootProject.file('key.properties')
           if (keystorePropertiesFile.exists()) {
               keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
           }

        android {
            compileSdkVersion 28

            sourceSets {
                main.java.srcDirs += 'src/main/kotlin'
            }

            lintOptions {
                disable 'InvalidPackage'
            }

            defaultConfig {
                // TODO: Specify your own unique Application ID 
               (https://developer.android.com/studio/build/application-id.html).
                applicationId "batuhan.todo"
                minSdkVersion 16
                targetSdkVersion 28
                versionCode flutterVersionCode.toInteger()
                versionName flutterVersionName
                testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
            }

               signingConfigs {
               release {
                   keyAlias keystoreProperties['keyAlias']
                   keyPassword keystoreProperties['keyPassword']
                   storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
                   storePassword keystoreProperties['storePassword']
               }
           }
           buildTypes {
               release {

                   signingConfig signingConfigs.release

               }
           }
        }

        flutter {
            source '../..'
        }

        dependencies {
            implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
            testImplementation 'junit:junit:4.12'
            androidTestImplementation 'androidx.test:runner:1.1.1'
            androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
            implementation 'com.google.firebase:firebase-analytics:17.2.0'
        }
我有key.jks,我添加到Firebase
sha-1
sha-256
,然后更新google-services.json。我跑得很干净。我在Firebase中使用Google启用了权限

如何解决此问题

我想我尝试了所有解决方案…

当应用程序的签名证书的SHA-1未在OAuth2客户端和应用程序的API密钥上正确配置时,会导致从Google登录引发“ApiException 10”


如果您在调试时运行应用程序,如前面评论中所述,则需要在Firebase项目的应用程序设置中添加。如果应用程序在发行版上运行,则应添加发行版密钥库中的SHA-1。

它说登录失败,请先检查您的登录实现,如果正确,请告诉我?如果您处于调试模式,则需要将调试密钥哈希添加到firebase控制台中,以便使用
flatter clean
        def localProperties = new Properties()
        def localPropertiesFile = rootProject.file('local.properties')
        if (localPropertiesFile.exists()) {
            localPropertiesFile.withReader('UTF-8') { reader ->
                localProperties.load(reader)
            }
        }

        def flutterRoot = localProperties.getProperty('flutter.sdk')
        if (flutterRoot == null) {
            throw new GradleException("Flutter SDK not found. Define location with 
        flutter.sdk in the local.properties file.")
        }

        def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
        if (flutterVersionCode == null) {
            flutterVersionCode = '1'
        }

        def flutterVersionName = localProperties.getProperty('flutter.versionName')
        if (flutterVersionName == null) {
            flutterVersionName = '1.0'
        }

        apply plugin: 'com.android.application'
        apply plugin: 'kotlin-android'
        apply plugin: 'com.google.gms.google-services'
        apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
         def keystoreProperties = new Properties()
           def keystorePropertiesFile = rootProject.file('key.properties')
           if (keystorePropertiesFile.exists()) {
               keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
           }

        android {
            compileSdkVersion 28

            sourceSets {
                main.java.srcDirs += 'src/main/kotlin'
            }

            lintOptions {
                disable 'InvalidPackage'
            }

            defaultConfig {
                // TODO: Specify your own unique Application ID 
               (https://developer.android.com/studio/build/application-id.html).
                applicationId "batuhan.todo"
                minSdkVersion 16
                targetSdkVersion 28
                versionCode flutterVersionCode.toInteger()
                versionName flutterVersionName
                testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
            }

               signingConfigs {
               release {
                   keyAlias keystoreProperties['keyAlias']
                   keyPassword keystoreProperties['keyPassword']
                   storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
                   storePassword keystoreProperties['storePassword']
               }
           }
           buildTypes {
               release {

                   signingConfig signingConfigs.release

               }
           }
        }

        flutter {
            source '../..'
        }

        dependencies {
            implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
            testImplementation 'junit:junit:4.12'
            androidTestImplementation 'androidx.test:runner:1.1.1'
            androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
            implementation 'com.google.firebase:firebase-analytics:17.2.0'
        }