Flutter:通过Google Firebase授权时出错 [英] Flutter:Error with authorization via Google Firebase

查看:70
本文介绍了Flutter:通过Google Firebase授权时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Flutter的乞gg,遇到了这样一个问题。我需要做到这一点,以便在登录我的应用程序时,我的邮件显示在firebase中。我做了某种授权,但是当我单击按钮(使用google登录)时出现错误。
这是我的pubspec.yaml:

 依赖项:
http:^ 0.12.0
抖动:
sdk:抖动
位置:^ 1.4.1
firebase_core:^ 0.2.5
firebase_auth:^ 0.5.20
google_sign_in:^ 3.2.2



您可以在此处找到更多信息: https://developers.google.com/android/guides/client-auth



您可以转到cmd并编写类似的内容以获取计算机的调试SHA密钥:
C:\Program Files\Java\jdk1.8.0_181\bin\keytool.exe -exportcert -list -v-别名androiddebugkey -keystore%USERPROFILE%\.android\debug.keystore


I am begginer in Flutter and faced such a problem.I need to make it so that when logging into my application, my mail is shown in the firebase. I kind of did authorization, but errors appear when I click on the button (login with google). Here is my pubspec.yaml:

dependencies:
  http: ^0.12.0
  flutter:
    sdk: flutter
  location: ^1.4.1 
  firebase_core: ^0.2.5
  firebase_auth: ^0.5.20
  google_sign_in: ^3.2.2

Here is the photo of problem

My project on GitHub

I can not get rid of this problem

import 'package:flutter/material.dart';
import 'package:logining/home_screen/home_screen.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';


class LoginScreen extends StatefulWidget {
  @override
  LoginScreenState createState() {
    return new LoginScreenState();
  }
}

class LoginScreenState extends State<LoginScreen> {
  String _email, _password;
  bool _obscureText = true;


GoogleSignIn googleAuth = GoogleSignIn();

  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Login'),
        ),
        resizeToAvoidBottomPadding: false,
        body: ListView(children: <Widget>[
          Container(
            child: Form(
              key: _formKey,
              autovalidate: true,
              child: Column(
                children: <Widget>[
                  SizedBox(
                    height: 50,
                  ),
                  Padding(
                    child: Image.asset(
                      'images/logo.png',
                      width: 100.0,
                      height: 100.0,
                    ),
                    padding: EdgeInsets.fromLTRB(50, 0, 50, 40),
                  ),
                  Padding(
                    padding: EdgeInsets.fromLTRB(25, 0, 50, 10),
                    child: TextFormField(
                      validator: (email) {
                        if (email.isEmpty) {
                          return 'Provide an Email';
                        }
                      },
                      style: TextStyle(
                        color: Color(0xFF01579B),
                        fontSize: 18.0,
                      ),
                      decoration: InputDecoration(
                          border: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(30),
                          ),
                          hintText: 'Enter your Email',
                          labelText: "Email",
                          icon: Padding(
                            padding: EdgeInsets.only(top: 20.0),
                            child: Icon(Icons.email),
                          )),
                      onSaved: (email) => _email = email,
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.fromLTRB(25, 0, 50, 10),
                    child: TextFormField(
                      validator: (password) {
                        if (password.isEmpty) {
                          return 'Provide an password';
                        }
                      },
                      obscureText: _obscureText,
                      style: TextStyle(
                        color: Color(0xFF01579B),
                        fontSize: 18.0,
                      ),
                      decoration: InputDecoration(
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(30),
                        ),
                        hintText: 'Enter your password',
                        labelText: "Password",
                        icon: Padding(
                          padding: EdgeInsets.only(top: 15.0),
                          child: Icon(Icons.lock),
                        ),
                        suffixIcon: GestureDetector(
                          onTap: () {
                            setState(() {
                              _obscureText = !_obscureText;
                            });
                          },
                          child: Icon(
                            _obscureText
                                ? Icons.visibility
                                : Icons.visibility_off,
                            semanticLabel: _obscureText
                                ? 'show password'
                                : 'hide password',
                          ),
                        ),
                      ),
                      onSaved: (password) => _password = password,
                    ),
                  ),
                  Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        Padding(
                          padding: EdgeInsets.symmetric(horizontal: 10),
                          child: RaisedButton(
                            color: Color(0xFFD50000),
                            textColor: Color(0xFFFFFFFF),
                            child: Text('Login with Google'),
                            onPressed: (){
                              googleAuth.signIn().then((result){
                                result.authentication.then((googleKey){
                                  FirebaseAuth.instance.signInWithGoogle(
                                    idToken: googleKey.idToken,
                                    accessToken: googleKey.accessToken

                                  ).then((signedInUser){
                                    print('Signed in as ${signedInUser.displayName}');
                                    Navigator.of(context).pushReplacementNamed('/list');
                                  });
                                }).catchError((onError){
                                  print(onError);
                                });
                                }).catchError((onError){
                                  print(onError);

                              });
                            },
                          ),

                        ),
                        Padding(
                          padding: EdgeInsets.symmetric(horizontal: 10),
                          child: RaisedButton(
                            color: Color(0xFF448AFF),
                            textColor: Color(0xFFFFFFFF),
                            child: Text('Login'),
                            onPressed: signIn,
                          ),
                        ),
                  ]),
                      Padding(
                          padding:
                              EdgeInsets.symmetric(horizontal: 10, vertical: 5),
                          child: FlatButton(
                            textColor: Color(0xFF448AFF),
                            child: Text('Forgot Password'),
                            onPressed: () {
                              print('onPressed');
                            },
                          ),
                        ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Padding(
                        padding: EdgeInsets.fromLTRB(50, 70, 0, 10),
                        child: Text(
                          'Still do not have an account ',
                          style: TextStyle(color: Color(0xFF9E9E9E)),
                        ),
                      ),
                      Padding(
                        padding: EdgeInsets.fromLTRB(0, 70, 30, 10),
                        child: FlatButton(
                          textColor: Color(0xFF448AFF),
                          child: Text('registration'),
                          onPressed: () {
                            Navigator.of(context).pushNamedAndRemoveUntil(
                                '/registration', (Route<dynamic> route) => false);
                          },
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ),
        ]),
      ),
    );
  }

void signIn() async {
    if(_formKey.currentState.validate()){
      _formKey.currentState.save();
      try{
        FirebaseUser user = await FirebaseAuth.instance.signInWithEmailAndPassword(email: _email, password: _password);
        Navigator.push(context, MaterialPageRoute(builder: (context) => HomeScreen(user: user)));
      }catch(e){
        print(e.message);
      }
    }
  }

}

解决方案

You need to add your SHA fingerprint over here:

You can find more information here: https://developers.google.com/android/guides/client-auth

You can go to cmd and write something like this to get the debug SHA key of your computer: "C:\Program Files\Java\jdk1.8.0_181\bin\keytool.exe" -exportcert -list -v -alias androiddebugkey -keystore %USERPROFILE%\.android\debug.keystore

这篇关于Flutter:通过Google Firebase授权时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆